Skip to content

Instantly share code, notes, and snippets.

@laktek
Created May 4, 2010 09:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save laktek/389202 to your computer and use it in GitHub Desktop.
Save laktek/389202 to your computer and use it in GitHub Desktop.
//run on Chrome's console
ws = new WebSocket("ws://localhost:3400");
ws.onmessage = function(ev){ console.log(ev.data); }
ws.send("hello");
//no response is received here :(//
var sys = require("sys");
var net = require("net");
var http = require("http");
function createTestServer(){
return new testServer();
};
function testServer(){
var server = this;
http.Server.call(server, function(){});
server.addListener("connection", function(){
// requests_recv++;
});
server.addListener("request", function(req, res){
res.writeHead(200, {"Content-Type": "text/plain"});
res.write("okay");
res.end();
});
server.addListener("upgrade", function(req, socket, upgradeHead){
socket.write( "HTTP/1.1 101 Web Socket Protocol Handshake\r\n"
+ "Upgrade: WebSocket\r\n"
+ "Connection: Upgrade\r\n"
+ "WebSocket-Origin: http://localhost:3400\r\n"
+ "WebSocket-Location: ws://localhost:3400/\r\n"
+ "\r\n"
);
request_upgradeHead = upgradeHead;
socket.ondata = function(d, start, end){
//var data = d.toString('utf8', start, end);
var original_data = d.toString('utf8', start, end);
var data = original_data.split('\ufffd')[0].slice(1);
if(data == "kill"){
socket.end();
} else {
sys.puts(data);
socket.write("\u0000test\uffff", "utf8");
}
};
});
};
sys.inherits(testServer, http.Server);
var server = createTestServer();
server.listen(3400);
@ThisIsMissEm
Copy link

The issue is the way that you are framing the data, currently you're sending \u0000 as plain UTF8 characters, (a backslash, a u, and 4 zeros). Try:

  socket.write("\u0000", "binary");
  socket.write(data, "utf8");
  socket.write("\uffff", "binary");

Although, I'd really recommend you use a WebSocket Server library, eg, http://github.com/miksago/node-websocket-server as the spec has now changed, and chrome will hopefully update their implementation soon.

@ThisIsMissEm
Copy link

Oh, also, on that repo, it's the Development branch you want, not master.

@laktek
Copy link
Author

laktek commented May 4, 2010

@miksago Thanks for the prompt reply. It worked and you're really awesome!

I didn't find a WebSocket Library that worked with the recent node.js, so that's why I started to write from the low-level. Anyway, seems your implementation would save my day.

@ThisIsMissEm
Copy link

Yeah, I've been working with ryan to add in certain things to node to make writing a websocket server more easy (like the upgrade event, great to see you using it there ;P )

As for my websocket-server library, I only develop to the latest HEAD builds, and in a few weeks time, I will hopefully have an early preview release of some extra stuff I'm adding to the server (channelling for instance) and also an implementation of draft76+, which includes a bunch of security updates to the protocol (means a lot more code)

The other thing with my websocket library is that it still works like a regular http.Server (apart from the first argument.), in order to listener for requests, you use the request event as used above. Also, the first argument to http.createServer should be optional.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment