WebSocket onpen函数不工作
WebSocket onpen function not working
我正在通过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);
}
}
}
您不能将ServerSocket与web套接字一起使用,您需要有一个支持侦听web套接字调用的web服务器,您可以在此处查看支持的web服务器。
支持的web套接字java服务器:
- http://jwebsocket.org/
- http://tomcat.apache.org/
- http://eclipse.org/jetty/
- http://grizzly.java.net/
- http://www.caucho.com/resin-web-server/
哪些流行的Web服务器支持HTML5WebSocket?
好的,我找到了解决方案,似乎收到的日期实际上是由于握手协议
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函数工作的时候
相关文章:
- 咖啡的Javascript订单总数无法使函数正常工作.请协助
- removeClass函数没有'工作不正常
- 你能解释一下这个阶乘函数是如何工作的吗?
- JavaScript链接在点击时不起作用;函数在页面加载时工作
- jQuery加载的async和ready函数不工作
- 更改智能表中的页面将停止JQuery函数的工作
- 为什么不是'我的函数在解析云代码中工作吗?当我在Angular和Express中测试时,它是有效的
- jQuery函数不能只在一个页面上工作
- 如何检查这个代码点火器php函数是否工作,该函数是否在ajax url中使用
- 从js引擎的角度来看闭包和构造函数是如何工作的
- appendChild在函数外部工作,但在函数内部不工作
- 如何在php中创建一个函数,该函数与文本区域一起工作,通过输入类似[color:red]的内容来打印具有等效颜色的文本
- 由于某种原因,我的JavaScript函数无法工作
- 为什么递归生成器函数没有't在ES2015工作
- 我需要一个jQuery函数来只工作在700px以上的屏幕大小,无法在我的代码中发现错误
- 从控制台中的工作函数转换为工作脚本.(请参阅编辑)
- Angular JS-工作函数中的“错误:[$interpole:interr]Can't interpole:
- 简化工作函数以替换特定单词
- 为什么不在回调中工作函数?
- InnerHTML不工作:函数未定义