java - Java 中的 WebSocket 编程 : client server communication issue

标签 java websocket

我正在尝试使用 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");
 }}

最佳答案

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

关于java - Java 中的 WebSocket 编程 : client server communication issue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35433324/

相关文章:

java - 如何在 Eclipse Java 动态 Web 项目中使用 .properties 文件?

java - 关于Java HashMap冲突的困惑

javascript - 如何使用 ActionCable websockets 处理 js 加载器?

cordova - 用于允许 Web 套接字的 content-security-policy 元标记

javascript - C# Websocket 远程方未完成关闭握手就关闭了WebSocket连接

security - 如何进行最安全的Websocket认证

java - 如何将@Before/@BeforeClass 与@Autowired 字段一起使用

java - RxJava 调度器的用例

java - 如果值不适合则设置字节

java - WebSocket 在 Firefox 中建立两个连接