I'm experimenting with HTML5 WebSockets and I got my basic client/server scenario working using Nugget. However, it only works when I execute a local HTML file (file:///
); as soon as I execute it through 开发者_运维百科http://localhost/
it fails.
So, connecting to localhost
from a file:///
HTML file works, but connecting to localhost
via http://localhost/
fails. This is weird and annoying, because I want to be able to use it in my home network.
I thought it would perhaps be because the origin is not the same (different ports), but this cannot be the cause as the ports must differ so as not to let Apache respond to WebSocket requests.
The client code itself is in its most basic form:
var ws;
function wsi() {
ws = new WebSocket('ws://localhost:8887/consoleappsample');
ws.onmessage = function (evt) {
alert(evt.data);
}
}
window.onload = wsi;
Does someone see what I might be doing incorrectly?
I managed to solve it in the end.
In the server, the following has to be added (*
instead of null
), replace x of course:
var nugget = new WebSocketServer(8887, "*", "ws://192.168.x.x:8887");
Then, in HandshakeHandler.cs, add this (where currently the last condition is not present):
if (hasRequiredFields && "ws://"+ClientHandshake.Host == Location && (ClientHandshake.Origin == Origin || Origin=="*"))
Thanks for all replies.
精彩评论