Firefox无法在ws:// localhost:8080 / websocket建立与服务器的连接

时间:2014-04-05 12:14:07

标签: java websocket

首先抱歉重复的问题,但我没有得到我想要的答案。我在控制台上收到上述错误

  

Firefox无法在ws:// localhost:8080 / websocket建立与服务器的连接。

这是我的jsp页面

   var username;
   var wsUri = "ws://localhost:8080/chat/websocket";
   var websocket = new WebSocket(wsUri);


   websocket.onopen = function (evt) {
       onOpen(evt);
   };
   websocket.onmessage = function (evt) {
       onMessage(evt);
   };
   websocket.onerror = function (evt) {
       onError(evt);
   };
   var output = document.getElementById("output");

   function join() {
       alert('join start');
       username = textField.value;
       websocket.send(username + " joined");
       alert('join end');
   }

   function send_message() {
       alert('sendmessage start');
       websocket.send(username + ": " + textField.value);
       alert('sendmessage end');
   }

   function onOpen() {
       alert('onopen start');
       writeToScreen("Connected to " + wsUri);
       alert('onopen end');
   }

   function onMessage(evt) {
       console.log("onMessage");
       writeToScreen("RECEIVED: " + evt.data);
       if (evt.data.indexOf("joined") !== -1) {
           userField.innerHTML += evt.data.substring(0, evt.data.indexOf(" joined")) + "\n";
       } else {
           chatlogField.innerHTML += evt.data + "\n";
       }
   }

   function onError(evt) {

       writeToScreen('<span style="color: red;">ERROR:</span> ' + evt.data);
   }

   function writeToScreen(message) {
       output.innerHTML += message + "<br>";
   }

这是我的java类

    @ServerEndpoint(value = "/websocket")
     public class ChatEndpoint {
        private static final Set < Session > peers = Collections.synchronizedSet(new HashSet < Session > ());

        @OnOpen
        public void onOpen(Session peer) {
            System.out.println("I am onOpen");
            peers.add(peer);
        }

        @OnClose
        public void onClose(Session peer) {
            System.out.println("I am onClose");
            peers.remove(peer);
        }

        @OnMessage
        public void message(String message, Session client) throws IOException, EncodeException {
            System.out.println("I am message");

            for (Session peer: peers) {
                peer.getBasicRemote().sendObject(message);
            }
        }
    }

我正在使用Maven存储库并尝试运行此项目以便客户端与java中的客户端交互。

0 个答案:

没有答案