java - Can not establish a WebSocket connection from JavaScript - Stack Overflow

The application is supposed to update a table whenever a request is made. I have the following code to

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.

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.
Share Improve this question edited May 13, 2014 at 6:22 Jack asked May 13, 2014 at 4:30 JackJack 6,66029 gold badges87 silver badges154 bronze badges 3
  • 1 One thing I can confirm that the issue is with your JS client and not the server. I dont have html 5 here so cannot test your JS, but I used a java based client and the server worked fine. – Hirak Commented May 13, 2014 at 4:43
  • How are you opening the HTML file? It should be serviced through a webserver on the same host as you want to connect to from javascript and not just opened as a file from disc, or else you will probably run into the security restrictions of the browser. – Gimby Commented May 13, 2014 at 7:53
  • I am running it on my localhost. Then how to test it before moving to server? – Jack Commented May 14, 2014 at 23:33
Add a ment  | 

1 Answer 1

Reset to default 3

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");
};

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744864945a4597920.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信