Created
May 4, 2010 09:39
-
-
Save laktek/389202 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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 :(// |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.