Skip to content

Instantly share code, notes, and snippets.

@lpinca
Created December 14, 2014 10:58
Show Gist options
  • Save lpinca/72b64fc08be9f045699d to your computer and use it in GitHub Desktop.
Save lpinca/72b64fc08be9f045699d to your computer and use it in GitHub Desktop.
Faye WebSocket ECONNRESET
(function () {
var ws = new WebSocket('ws://localhost:3000');
ws.onopen = function open() {
console.log('open');
ws.send('Hello, world!');
};
ws.onmessage = function message(event) {
console.log(event.data);
};
ws.onclose = function close(event) {
console.log('close', event.code, event.reason);
ws = null;
};
})();
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<script src="/client.js"></script>
</body>
</html>
{
"name": "gh-321",
"version": "1.0.0",
"description": "",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "MIT",
"dependencies": {
"faye-websocket": "^0.9.0",
"permessage-deflate": "^0.1.0"
}
}
'use strict';
var deflate = require('permessage-deflate')
, Faye = require('faye-websocket')
, http = require('http')
, fs = require('fs');
var server = http.createServer();
server.on('request', function request(req, res) {
if (req.url === '/client.js') {
res.writeHead(200, { 'Content-Type': 'application/javascript' });
fs.createReadStream(__dirname + '/client.js').pipe(res);
return;
}
res.writeHead(200, { 'Content-Type': 'text/html' });
fs.createReadStream(__dirname + '/index.html').pipe(res);
});
server.on('upgrade', function upgrade(req, socket, head) {
if (!Faye.isWebSocket(req)) return socket.destroy();
var ws = new Faye(req, socket, head, null, {
extensions: [ deflate ]
});
ws.on('open', function open() {
ws.on('message', function echo(event) {
ws.send(event.data);
});
ws.on('error', function error(err) {
console.error(err);
});
ws.on('close', function close(event) {
console.log('close', event.code, event.reason);
ws = null;
});
});
});
server.listen(3000, function listening() {
var bound = server.address();
console.log('listening on %s:%s', bound.address, bound.port);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment