Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var sys = require("sys"),
- ws = require("./ws");
- Array.prototype.remove = function(e) {
- for (var i = 0; i < this.length; i++)
- if (e == this[i]) return this.splice(i, 1);
- }
- var clients = [];
- ws.createServer(function (websocket) {
- clients.push(websocket);
- websocket.addListener("connect", function (resource) {
- // emitted after handshake
- sys.debug("connect: " + resource);
- // server closes connection after 10s, will also get "close" event
- //setTimeout(websocket.end, 10 * 1000);
- }).addListener("data", function (data) {
- // handle incoming data
- // send data to client
- clients.forEach(function (client) {
- if (client != websocket) {
- sys.puts('write...');
- try {
- client.write(data);
- } catch (e) {
- clients.remove(client);
- }
- }
- });
- }).addListener("close", function () {
- // emitted when server or client closes connection
- sys.debug("close");
- clients.remove(websocket);
- });
- }).listen(8000);
Add Comment
Please, Sign In to add comment