Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can not establish a WebSocket connection from JavaScript

The application is supposed to update a table whenever a request is made. I have the following code to receive notifications from server. When I run the application it shows followings in alert boxes, seems it is connected but when I call 'send' method of the notification class it does not change anything.

Alert 1)

       windowfunction connect() {
            wsocket = new WebSocket("ws://localhost:8080/Notifications");
            alert("got connected");
            document.getElementById("foo").innerHTML = "arraypv[0]";
            wsocket.onmessage = onMessage;
        }

Alert 2)

      got connected 

JavaScript

 <script type="text/javascript">
            var wsocket;
            function connect() {
                wsocket = new WebSocket("ws://localhost:8080/Notifications");
                alert("got connected");
                wsocket.onmessage = onMessage;
            }
            function onMessage(evt) {
                alert(evt);
                var arraypv = evt;
                alert("array" + arraypv);
                document.getElementById("foo").innerHTML = arraypv[0];
            }
            alert("window" + connect);
            window.addEventListener("load", connect, false);
        </script>

Code

@ServerEndpoint("/Notifications")
public class Notifications {

   /* Queue for all open WebSocket sessions */
   static Queue<Session> queue = new ConcurrentLinkedQueue();


   public static void send() {
       System.err.println("send");
       String msg = "Here is the message";
      try {
         /* Send updates to all open WebSocket sessions */
         for (Session session : queue) {
            session.getBasicRemote().sendText(msg);
         }
      } catch (IOException e) {
         e.printStackTrace();
      }
    }

   @OnOpen
    public void openConnection(Session session) {
        System.err.println("in open connection");
        queue.add(session);
    }

    @OnClose
    public void closedConnection(Session session) {
        System.err.println("in closed connection");
        queue.remove(session);
    }

    @OnError
    public void error(Session session, Throwable t) {
        System.err.println("in error");
        queue.remove(session);
    }
}

Maven

 <dependency>
            <groupId>javax.websocket</groupId>
            <artifactId>javax.websocket-api</artifactId>
            <version>1.0-b08</version>
        </dependency>

To send a message I use following code in one of my functions

     Notifications.send();

Console just shows

SEVERE: send

When I trace the connection using FireBug it shows

Firefox can't establish a connection to the server at ws://localhost:8080/Notifications.
like image 286
Jack Avatar asked Aug 27 '26 07:08

Jack


1 Answers

Missing Trailing Slash

You forgot the trailing slash: You should connect to

ws://localhost:8080/Notifications/ 

instead of

ws://localhost:8080/Notifications

(Note the trailing slash which is really important).

Also, there are some more problems with your code. WebSocket is - like almost everything in javascript asynchroneous. At the point of time where you do your

alert("got connected");

the websocket is not actaully connected. Please attach an eventhandler to it like this

wsocket.onopen = function() {
    alert("got connected");
};
like image 68
Prior99 Avatar answered Aug 29 '26 21:08

Prior99



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!