我正在开发一个websocket应用程序。我有一个用C#编写的服务器。我使用另一个C#应用程序对其进行了测试,用于发送和接收数据。当我在chrome工具(控制台)上使用JavaScript并使用Websockets连接到我的服务器时,就会出现这个问题。
议题:-
- But when I send a disconnect response from the server, the websocket triggers the onclose method. (So I assume that he was open but not ready to communicate)
有什么建议吗?这是JavaScript,如果有帮助的话。
websocket = new WebSocket('ws://My server IP here:8080');
try {
websocket.onopen = function(evt) {
open(evt)
//websocket.send("Message to send");
alert("Message is sent...");
}
}
catch(err) {
debug(err,'error')
}
websocket.onerror = function(evt) {
error(evt)
}
websocket.onclose = function(evt) {
close(evt)
}
websocket.onmessage = function(evt) {
message(evt)
}
function open(evt) {
alert("CONNECTED");
doSend("WebSocket rocks");
}
function error(evt) {
alert (evt.data)
}
function close(evt) {
alert("DISCONNECTED");
}
function message(evt) {
alert(evt.data);
}
function doSend(message) {
alert(message);
websocket.send(message);
}我发回的头像
HTTP/1.1 101 WebSocket协议握手 升级: WebSocket 连接:升级 Sec-WebSocket-原产地: chrome://newtab Sec-WebSocket-位置:ws://我的服务器IP:8080?
谢谢大家。
发布于 2011-12-07 04:38:33
看起来,您是在尝试响应握手请求,而没有适当的挑战响应。正如Robin提到的,握手现在变得更加复杂,并且涉及到对WebSocket协议新版本的挑战。这是一篇好文章。,它更详细地解释了76版握手。
下面是一个代码示例,它检测WebSocket协议版本并使用适当的响应进行响应。(它是用Java编写的,所以是YMMV,但它应该指向正确的方向。)
// Create the WebSocket handshake response.
HttpResponse res = new DefaultHttpResponse(HTTP_1_1,
new HttpResponseStatus(101, "Web Socket Protocol Handshake"));
res.addHeader(Names.UPGRADE, WEBSOCKET);
res.addHeader(CONNECTION, Values.UPGRADE);
// Fill in the headers and contents depending on handshake method.
// New handshake specification has a challenge.
if (req.containsHeader(SEC_WEBSOCKET_KEY1)
&& req.containsHeader(SEC_WEBSOCKET_KEY2)) {
// New handshake method with challenge
res.addHeader(SEC_WEBSOCKET_ORIGIN, req.getHeader(ORIGIN));
res.addHeader(SEC_WEBSOCKET_LOCATION, getWebSocketLocation(req));
String protocol = req.getHeader(SEC_WEBSOCKET_PROTOCOL);
if (protocol != null) {
res.addHeader(SEC_WEBSOCKET_PROTOCOL, protocol);
}
// Calculate the answer of the challenge.
String key1 = req.getHeader(SEC_WEBSOCKET_KEY1);
String key2 = req.getHeader(SEC_WEBSOCKET_KEY2);
int a = (int) (Long.parseLong(key1.replaceAll("[^0-9]", "")) / key1
.replaceAll("[^ ]", "").length());
int b = (int) (Long.parseLong(key2.replaceAll("[^0-9]", "")) / key2
.replaceAll("[^ ]", "").length());
long c = req.getContent().readLong();
ChannelBuffer input = ChannelBuffers.buffer(16);
input.writeInt(a);
input.writeInt(b);
input.writeLong(c);
ChannelBuffer output = ChannelBuffers
.wrappedBuffer(MessageDigest.getInstance("MD5").digest(
input.array()));
res.setContent(output);
} else {
// Old handshake method with no challenge:
res.addHeader(WEBSOCKET_ORIGIN, req.getHeader(ORIGIN));
res.addHeader(WEBSOCKET_LOCATION, getWebSocketLocation(req));
String protocol = req.getHeader(WEBSOCKET_PROTOCOL);
if (protocol != null) {
res.addHeader(WEBSOCKET_PROTOCOL, protocol);
}
}
// Send the response...发布于 2011-02-19 11:35:53
您是否实现了正确的Websockets版本?Chrome有移至第76版,这意味着握手比以前更复杂。如果Javascript客户端未能正确连接,这可能是原因之一。
https://stackoverflow.com/questions/5043685
复制相似问题