【问题标题】:WebSocket onpen function not workingWebSocket onopen 功能不起作用
【发布时间】:2013-12-16 11:57:16
【问题描述】:

我正在通过 Web 套接字向 Java 服务器发送文本,但从未调用过 onpen 函数,这是用于客户端 (WebSocketTest) 的函数,当我关闭服务器时,onclose 函数的警报消息正确调用

function WebSocketTest()
{
  if ("WebSocket" in window)
  {

     alert("WebSocket is supported by your Browser!");
     // Let us open a web socket
     var ws = new WebSocket("ws://localhost:4444");
     ws.onopen = function()
     {
        // Web Socket is connected, send data using send()
        ws.send("Message to send");
        alert("Message is sent...");
     };
     ws.onmessage = function (evt) 
     { 
        var received_msg = evt.data;
        alert("Message is received...");
     };
     ws.onclose = function()
     { 
        // websocket is closed.
        alert("Connection is closed..."); 
     };
  }
  else
  {
     // The browser doesn't support WebSocket
     alert("WebSocket NOT supported by your Browser!");
  }
}

这是服务器收到的内容

GET / HTTP/1.1
Upgrade: websocket
Connection: Upgrade
Host: localhost:4444
Origin: null
Pragma: no-cache
Cache-Control: no-cache
Sec-WebSocket-Key: 4BAiV8AU80juonjYQw5V9g==
Sec-WebSocket-Version: 13
Sec-WebSocket-Extensions: x-webkit-deflate-frame
User-Agent: Mozilla/5.0 (Windows NT 6.2) AppleWebKit/537.36 (KHTML, like Gecko)      Chrome/31.0.1650.63 Safari/537.36

这是服务器端

public class InvoiceListener {

    private static BufferedReader in;
    private final static int port = 4444;
    private static ServerSocket listenSocket;
    private static Socket client;
    private static Invoice invoice;
    private static String info;

    public static void main(String[] args) throws IOException {

        PrintInvoice printer;
        ServerSocket listenSocket = new ServerSocket(port);


            System.out.println("Listening");
            client = listenSocket.accept();
            System.out.println("client connected !");
            in = new BufferedReader(new InputStreamReader(
                    client.getInputStream()));
            while ((info = in.readLine()) != null) {
                System.out.println(info);
            }

    }

}

【问题讨论】:

  • 你使用的是什么网络服务器?
  • java,使用java ServerSocket
  • 您测试的浏览器是什么?什么版本?
  • 谷歌浏览器版本 31.0.1650.63 m
  • 你能用更多代码更新你的帖子吗?我的意思是你是如何在服务器(java)上接收请求的,你什么时候在你的页面中调用这个函数?

标签: java javascript html websocket client


【解决方案1】:

您不能将ServerSocket与web socket一起使用,您需要有一个支持监听web socket调用的web服务器,您可以在这里查看支持的web服务器。

支持的 web socket java 服务器:

What popular webservers have support for HTML5 WebSocket?

【讨论】:

    【解决方案2】:

    好的,我找到了解决方案,似乎收到的日期实际上是由于握手协议引起的

    GET / HTTP/1.1
    Upgrade: websocket
    Connection: Upgrade
    Host: localhost:4444
    Origin: null
    Pragma: no-cache
    Cache-Control: no-cache
    Sec-WebSocket-Key: 4BAiV8AU80juonjYQw5V9g==
    Sec-WebSocket-Version: 13
    Sec-WebSocket-Extensions: x-webkit-deflate-frame
    User-Agent: Mozilla/5.0 (Windows NT 6.2) AppleWebKit/537.36 (KHTML, like Gecko)
    

    我必须再次从服务器向客户端发送响应以启动连接,这就是 Onopen 功能起作用的时候

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-14
      • 1970-01-01
      • 1970-01-01
      • 2023-03-13
      相关资源
      最近更新 更多