WebSockets无法与服务器建立连接

时间:2013-02-01 13:05:31

标签: eclipse tomcat websocket

我正在运行WebSockets的简单Echo示例,但它似乎没有建立连接。这种情况发生在Windows 7,Eclipse和Tomcat上。

firefox中的错误消息是:

"NetworkError: 404 Not Found - http://my_ip_address:8080/websockets/echo"

Firefox can't establish a connection to the server at ws://my_ip_address:8080/websockets/echo.

我错过了什么?我需要设置一些配置吗?

我在构建路径中拥有的jar是javax.annotation_1.0.jar and websocket-api-0.2.jar

我正在运行的代码:

package org.javaee.glassfishwebsocket;
import org.glassfish.websocket.api.annotations.WebSocket;
import org.glassfish.websocket.api.annotations.WebSocketMessage;

@WebSocket(path="/echo")
public class EchoBean {
    @WebSocketMessage
    public String echo(String message) {
        System.out.println("##################### Message received");
        return message + " (from your server)";
    }     
}

这是index.jsp:

<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
</head>

<body>
    <meta charset="utf-8">
    <title>WebSocket Echo Client</title>
    <script language="javascript" type="text/javascript">

        console.log("trying to connect");

        var wsUri = "ws://my_ip_address:8080/websockets/echo";
        console.log("wsUri = ");
        console.log(wsUri);
        var websocket = new WebSocket(wsUri);
        console.log("websocket = " );
        console.log(websocket);
        websocket.onopen = function(evt) { onOpen(evt) };
        websocket.onmessage = function(evt) { onMessage(evt) };
        websocket.onerror = function(evt) { onError(evt) };

        function init() {
            console.log("init called");
            output = document.getElementById("output");
        }

        function send_echo() {
            console.log("sending_echo with " + textID.value);
            websocket.send(textID.value);
            writeToScreen("SENT: " + textID.value);
        }

        function onOpen(evt) {
            console.log("onOpen");
            writeToScreen("CONNECTED");
        }

        function onMessage(evt) {
            console.log("onMessage");
            writeToScreen("RECEIVED: " + evt.data);
        }

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

        function writeToScreen(message) {
            var pre = document.createElement("p");
            pre.style.wordWrap = "break-word";
            pre.innerHTML = message;
            output.appendChild(pre);
        }

        window.addEventListener("load", init, false);
    </script>

    <h2 style="text-align: center;">WebSocket Echo Client</h2>
    <div style="text-align: center;"><img style=" width: 64px; height: 64px;" alt=""src="HTML5_Logo_512.png"></div>
    <br></br>
    <div style="text-align: center;">
        <form action=""> 
            <input onclick="send_echo()" value="Press me" type="button"> 
            <input id="textID" name="message" value="Hello WebSocket!" type="text"><br>
        </form>
    </div>
    <div id="output"></div>
</body>
</html>

0 个答案:

没有答案