添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

Heyo,

I’m giving websockets another try and I want to have it working on my localhost Lucee 5.3.3.62. I’m following this github: link

What have I done:
I installed it in the web environment through extensions version 2.0.3. Added the servlet jar file. Added the url pattern in the web.xml. Made a simple example following the video on the page.

What is the error
When I call the example page then I see the page but get the following error:

index.cfm:15 WebSocket connection to ‘ws://localhost:8888/ws/echo’ failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED

Where I think the problem lies
I go to this page using this url “localhost/chattest/index.cfm”. But in the example he goes to “localhost:8888/etc.”. When I go to “localhost:8888” I get the following error:

So I think somethings wrong with the port 8888. I tried to find out if there are other applications using this port, but it seems not to be the case. Is it something I have to do in Tomcat? Can’t find many answers online. I’ve also have a websocket.log file, but nothing showing up there.

If somebody could help me point me in the right direction then that would be very helpful

Thanks in advance,
DrunkenMoose

Can you try replacing localhost with “127.0.0.1” in your code?

Also, keep in mind localhost/chattest/index.cfm is NOT the same as localhost:8888/chattest/index.cfm (first is port 80, second is port 8888) and that’s probably why you’re getting the “Deze site is niet bereikbaar” message. Sounds like using localhost/chattest/index.cfm (or 127.0.0.1/chattest/index.cfm) is what you should be doing, and using port 8888 to connect to ws in your code (something like ws://127.0.0.1:8888/ws/echo, i think).

If you can provide us your code and web.xml file, we can help debug more.

Thanks for your response. I also tried “127.0.0.1” but every combination gives the same error. I think it has something to do with the fact I cant get 127.0.0.1:8888 working. Here is the code to the index page:
templates - Codeshare and here is the code to the xml: Apr 25 4:29 AM - Codeshare . The listener is as followed:

component {
function onMessage(websocket, message, sessionScope, applicationScope){
return “Echo from Lucee #Server .lucee.version# [ #arguments .message#]”;

And if I do as you said then I get the ERR_CONNECTION_REFUSED error. Sound more like a general error that something is blocked.

Greetings,
DrunkenMoose

Yea my question is where is the setting for this port and how can I get 8888 working? I tried the port 80 but then you get a 404. And with port 8888 i get the next error connection refused. But if you look at the example in the websocket github then you see he uses port 8888. I think this is the port the websocket can use, if correctly installed.

I think he is using :8888 because that’s the port on which his Lucee is running. Do you have Apache or NGinx running in front of Lucee? What port is your Lucee running on?

What is the exact error message you receive (check console.log). Try the following command in console with various ports:

var wsecho = new WebSocket(“ws://localhost:8080/ws/echo”);

If you get: VM524:1 WebSocket connection to ‘ws://localhost:8080/ws/echo’ failed: Error during WebSocket handshake: Unexpected response code: 404 , then that means your WS didn’t register properly or your files are not in the correct location.

If you get: VM530:1 WebSocket connection to ‘ws://localhost/ws/echo’ failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED , then it’s probably the wrong port.

Well Lucee itself runs on port 80. This is the only port that works when I try to open the page in the browser. I’ve put it all in one screenshot:

screenshot 961×445 24.3 KB

edit: also tried with 127.0.0.1 again

Thanks!

So for you, port 80 is the correct port.
I installed per the README file at GitHub - isapir/lucee-websocket: Enables server WebSockets for Lucee via JSR-356 compliant servlet containers (e.g. Tomcat 8, Jetty 9.1, etc.)
I put servlet-filter-utils-1.1.1.jar in tomcat lib directory
I added required snippet to the web.xml file in tomcat/webapps/lucee-5.2.2.71/WEB-INF/web.xml
Restarted tomcat and the EchoListener example works for my Lucee port 8080

I didn’t do anything with websocket yet, but from looking into your web.xml (shared on codeshare) and Igals instructions: I am missing the filter section in your web.xml.

I want to try websockets also, it is on my todos list. Going to post back then.

I recently implemented WebSockets on an Ubuntu Apache web-server having WebMin and configured with http-2 and here are my notes:

STEPS FOR WEBSOCKETS : install the lucee websocket plugin /lucee/admin/server.cfm?action=ext.applications … then download the file servlet-filter-utils-1.1.1.jar from Releases · isapir/lucee-websocket · GitHub and put it in the {tomcat}/lib directory of the lucee install … next you need to activate the ws:// and wss:// protocols in apache: sudo a2enmod proxy_wstunnel … then add to the {tomcat}/conf/web.xml file:

<!-- Required for the Lucee WebSocket Extension !-->    
<filter>
  <filter-name>HttpSessionInitializerFilter</filter-name>
  <filter-class>net.twentyonesolutions.servlet.filter.HttpSessionInitializerFilter</filter-class>
</filter>
<filter-mapping>
  <filter-name>HttpSessionInitializerFilter</filter-name>
  <!-- modify url-pattern to match your websocket endpoints !-->
  <url-pattern>/ws/*</url-pattern>
</filter-mapping>

… next add to the apache2 global config Webmin > Servers > Apache Webserver > Global configuration > Edit Config Files :

**ref Only**:<IfModule mod_proxy.c>
**ref Only**:ProxyPreserveHost On
**add this**:ProxyPassMatch ^/ws/(.*)$ ws://127.0.0.1:8888/ws/$1
**Other Stuff Here ... **
**ref Only**:ProxyPassReverse / http://127.0.0.1:8888/
**ref Only**:</IfModule>

Finally add websocks to the log settings: /lucee/admin/server.cfm?action=server.logging {ame:“websocket”,appender:“Resource”,layout:“classic”,level:“debug”}

So I’ve played a little with this extension, and everything seems to go pretty well just as described in Igal Sapirs instructions.

What I understand from your post is that in your configuration you can access Lucee on port 80, but not on port 8888? That means that, or you have:

  • a webserver (like Apache or IIS)) in front of Lucee and Lucee is not running on its default port:8888, or
  • Lucee is running as stand alone on port:80
  • To identify that, please look for the port Lucee/Tomcat is running on, by opening your server.xml at {installation path}\lucee\conf\server.xml and search for connector port". If its 8888 then you should be able to access http://localhost:8888 without any problem, unless something else is conflicting (e.g. firewall, another webserver or service running on port 8888, or hardcoded hosts-file entry for localhost pointing to another IP).

    Here I am posting my step by step instructions of what I’ve done:

    My System:
    OS: Win10
    Java: Zulu 8.36.0.1-CA-win64 (build 1.8.0_202-b05)
    
  • Downloaded the latest stable Express Version 5.3.3.62
  • Started Lucee and logged into server-admin at: http://localhost:8888/lucee/admin/server.cfm
  • From that server-admin-page at Extensions => Applications I installed Igals websocket extension 2.0.3
  • Downloaded servlet-filter-utils-1.1.1.jar from
    https://github.com/isapir/lucee-websocket/releases/download/2.0.2/servlet-filter-utils-1.1.1.jar to the folder *{installation path}\lucee\lib\ext*
  • Stopped Lucee
  • Deleted all the files/folders inside the webroot:
    {installation path}\lucee\webapps\ROOT\
  • Added the following code just before the <welcome-file-list> tag at the bottom of {installation path}\lucee\conf\web.xml:
  •  <!-- Required for the Lucee WebSocket Extension !-->    
        <filter>
          <filter-name>HttpSessionInitializerFilter</filter-name>
          <filter-class>net.twentyonesolutions.servlet.filter.HttpSessionInitializerFilter</filter-class>
        </filter>
        <filter-mapping>
          <filter-name>HttpSessionInitializerFilter</filter-name>
          <!-- modify url-pattern to match your websocket endpoints !-->
          <url-pattern>/ws/*</url-pattern>
        </filter-mapping>
    
  • Created the three files:
    {installation path}\lucee\webapps\ROOT\ChatListener.cfc
    {installation path}\lucee\webapps\ROOT\Application.cfc
    just as posted from the wikis chat-example at:
    Example Chat · isapir/lucee-websocket Wiki · GitHub
    For {installation path}\lucee\webapps\ROOT\index.cfm try this slightly modified code:
  • <!--- index.cfm --->
    <cfscript>
        /** ensure that we have Session.username and set a default channel */
        if (!isNull(URL.username))
            Session.username = URL.username;
        if (isEmpty(Session.username ?: "")){
            echo("<p>Session.username is not defined. Set it using the URL parameter username, e.g. ?username=Lucy");
            abort;
        param name="channel" default="default";
    </cfscript>
    <cfoutput>
        <!--- JavaScript follows !--->
        <script>
            function sendText(){
                var texttosend=document.getElementById("texttosend").value;
                wschat.send(texttosend);
                document.getElementById("texttosend").value='';
            var channel  = "#channel#";
            var endpoint = "/ws/chat/" + channel;
            var protocol = (document.location.protocol == "https:") ? "wss://" : "ws://";
            var wschat   = new WebSocket(protocol + document.location.host + endpoint);
            var log = function(evt){
                console.log(evt.data ? JSON.parse(evt.data) : evt);
            var writemessage=function(evt){
                console.log(evt.data ? JSON.parse(evt.data) : evt);
                if(evt.data){
                    res=JSON.parse(evt.data);
                    document.getElementById("chatcontent").insertAdjacentHTML("afterbegin", res.FROM+': <b>'+ res.MESSAGE+'</b> <i>at '+res.TIMESTAMP+'</i></br>');
            wschat.onopen    = log;
            wschat.onmessage = writemessage;
            wschat.onerror   = log;
            wschat.onclose   = log;
            document.title   = `#Session.username# on ${channel}`;
            document.write(`<p>[wschat] connected to "${channel}" as "#Session.username#"`);
        </script>
    </cfoutput>
    <style type="text/css">#inputfield{position:fixed;bottom: 0; left:0;padding:30px;} #texttosend{width:100%;}</style>
        <div id="chatcontent"></div>
        <div id="inputfield">
            <input type="text" id="texttosend">
            <button onClick="sendText();">Send</button>
    </body>
    </html>
    
  • Started Lucee
  • Opened in Chrome: http://localhost:8888/?username=User1 and opened its WebDevTool by pressing F12 (with the console-view)
  • Opened in Firefox http://localhost:8888/?username=User2 and opened its WebDevTool by pressing F12 (with the console-view)
  • Entered in Chromes WebDevTool Console console>wschat.send('This is a test');
  • Confirmed the receipt of the message within the Console of Firefox
  • Igals websocket works as espected. Important to know is that this chat example is not a 100% finished chat. It just sends messages and receives JSON data and logs it to the respective consoles. It’s up to you to create a front end with that.

    Hope that helps a little.

    Thank you for this extended answer! That probably a big help for many.
    I’m a bit busy this week, but I hope to continue playing with this in the weekend.

    I made a whole front-end/back-end couple of years ago, but we’ve never got to use it because
    the websocket weren’t reliable. On random moments the messages weren’t coming through.

    So I wanted to do a clean setup to check if I can get it working again. I will follow your answer step-by-step and come with a response. But thanks in advance!

    Hey again,

    I couldn’t help myself, so I took a quick look:

    So like I said in my previous posts that I think the problem is that I cannot access port 8888.
    You said to look for the port in the server.xml. I found it and it was on 80, changed that to 8888.
    And now it’s working… so thanks!

    Now I will look if I can enable this is our bigger projects.