Last active
August 29, 2015 14:08
-
-
Save lpinca/08d409cedb9d8e836939 to your computer and use it in GitHub Desktop.
Faye WebSocket Y U NO EXIT (‡▼益▼)
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 ws = require('ws') | |
, http = require('http'); | |
var server = http.createServer(); | |
var wss = new ws.Server({ noServer: true }); | |
server.on('upgrade', function (req, socket, head) { | |
setTimeout(function () { | |
wss.handleUpgrade(req, socket, head, function (socket) { | |
socket.on('message', function (message) { | |
socket.send(message); | |
}); | |
socket.on('close', function (code, reason) { | |
console.log('server close', code, reason); | |
}); | |
}); | |
}, 1000); | |
}); | |
server.listen(3000, function () { | |
var socket = new ws('ws://localhost:3000/'); | |
socket.on('open', function () { | |
console.log('open'); | |
socket.send('Hello, world!'); | |
}); | |
socket.on('message', function (data) { | |
console.log('message', data); | |
}); | |
socket.on('close', function () { | |
console.log('client close'); | |
}); | |
setTimeout(function () { | |
socket.close(); | |
server.close(function () { | |
console.log('bye'); | |
}); | |
}, 200); | |
}); |
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 Faye = require('faye-websocket') | |
, http = require('http'); | |
var server = http.createServer(); | |
server.on('upgrade', function (req, socket, body) { | |
setTimeout(function () { | |
if (!Faye.isWebSocket(req)) return socket.destroy(); | |
var ws = new Faye(req, socket, body); | |
ws.on('message', function (event) { | |
ws.send(event.data); | |
}); | |
ws.on('close', function (event) { | |
console.log('server close', event.code, event.reason); | |
ws = null; | |
}); | |
}, 1000); | |
}); | |
server.listen(3000, function () { | |
var ws = new Faye.Client('ws://localhost:3000/'); | |
ws.on('open', function (event) { | |
console.log('open'); | |
ws.send('Hello, world!'); | |
}); | |
ws.on('message', function (event) { | |
console.log('message', event.data); | |
}); | |
ws.on('close', function (event) { | |
console.log('client close', event.code, event.reason); | |
ws = null; | |
}); | |
setTimeout(function () { | |
ws.close(); | |
server.close(function () { | |
console.log('bye'); | |
}); | |
}, 200); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment