【问题标题】:WebSocket Programming in Java: client server communication issueJava中的WebSocket编程:客户端服务器通信问题
【发布时间】:2016-02-17 13:17:17
【问题描述】:

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

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

谁能帮帮我?

网络服务器: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");
 }}

【问题讨论】:

  • 那么客户端和服务器上打印了哪些日志?
  • @wero 目前没有日志,我认为在“function start()”之后没有任何效果
  • 我明天会给你发一个工作示例(如果还没有解决的话)。
  • @Gero 是的,我们仍在努力。

标签: java websocket


【解决方案1】:

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

【讨论】:

  • 感谢捕获,但仍然没有输出。我还有一个问题是服务器类是普通的Java类,在这种情况下我每次都需要部署war文件吗?因为我目前正在做的只是从eclipse执行上面的jsp页面。
  • 通过更新Typo webSocket --> websocket,程序正常运行。
【解决方案2】:

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

查看本教程http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/HomeWebsocket/WebsocketHome.html

【讨论】:

  • 感谢详细的代码,让我试一试。无论如何都会反馈[PASS/FAIL]
  • hi Ivan,通过更新 Typo webSocket --> websocket,程序可以正常运行。
猜你喜欢
  • 1970-01-01
  • 2021-11-10
  • 1970-01-01
  • 2016-02-12
  • 1970-01-01
  • 2015-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多