Java中的WebSocket编程:客户端服务器通信问题

时间:2016-02-16 13:00:26

标签: java websocket

我正在尝试使用Java Web Application实现简单的WebSocket程序。

但是,无法在客户端和服务器之间建立通信。

有人能帮助我吗?

Web服务器:Tomcat

客户端代码: jsp / javascrip

<body>
<div>
    <input type="text" value="" id="message" /> <br /> <input
        type="submit" value="Start" onclick="start()" />
</div>
<div id="messages"></div>
<script type="text/javascript">
    var webSocket;
    var uri = 'ws://' + window.location.host + '/ZebraHosting/testwebsocket';
    alert('ur url is ' + uri);
    function connect() {

        if ('WebSocket' in window) {
            alert('I am in Websocket in window');
            websocket = new WebSocket(uri);
        } else if ('MozWebSocket' in window) {
            websocket = new MozWebSocket(uri);
            alert('I am in MozWebsocket in window');
        } else {
            alert('WebSocket is not supported by this browser.');
            return;
        }

        webSocket.onerror = function(event) {
            alert('I am onerror');
            onError(event);
        };

        webSocket.onopen = function(event) {
            alert('I am onopen');
            onOpen(event);
        };

        webSocket.onmessage = function(event) {
            alert('I am onmessage');
            onMessage(event);
        };

        webSocket.onclose = function(event) {
            alert('I am onclose');
            onClose(event);
        };

    }
    function onMessage(event) {
        document.getElementById('messages').innerHTML += '<br />'
                + event.data;
    }

    function onOpen(event) {
        alert("function onOpen " );
        document.getElementById('messages').innerHTML = 'Connection established';
    }

    function onError(event) {
        alert("Error ocurred " );
    }

    function start() {
        alert("function start " );
        webSocket.send(document.getElementById('message').value);
        return false;
    }

    function onClose(event) {
        alert("function onClose" );
        document.getElementById('messages').innerHTML = 'Connection closed';
    }

    connect();
</script>

服务器代码:

import java.io.IOException;
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;

@ServerEndpoint("/testwebsocket")
public class WebSocketTest {

@OnMessage
public void onMessage(String message, Session session) throws IOException, InterruptedException {

    // Print the client message for testing purposes
    System.out.println("Received: " + message);
    // Send the first message to the client
    session.getBasicRemote().sendText("replay from server for :" + message);
}

@OnOpen
public void onOpen() {
    System.out.println("Client connected");
}

@OnClose
public void onClose() {
    System.out.println("Connection closed");
 }}

2 个答案:

答案 0 :(得分:3)

您的代码中存在拼写错误:您创建WebSocket个对象并将其分配给变量websocket,但稍后使用变量webSocket

答案 1 :(得分:1)

我认为服务器端类缺少@ApplicationScoped注释。

请参阅本教程http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/HomeWebsocket/WebsocketHome.html