Created
December 7, 2014 14:08
-
-
Save lpinca/73196200496efb3222fe to your computer and use it in GitHub Desktop.
permessage-deflate write after end
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
(function () { | |
var ws = new WebSocket('ws://localhost:3000'); | |
ws.onopen = function () { | |
ws.send('shutdown'); | |
}; | |
})(); |
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
</head> | |
<body> | |
<script src="client.js"></script> | |
</body> | |
</html> |
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
{ | |
"name": "permessage-deflate", | |
"version": "1.0.0", | |
"description": "", | |
"main": "server.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1", | |
"start": "node server.js" | |
}, | |
"author": "", | |
"license": "MIT", | |
"dependencies": { | |
"ws": "0.6.0" | |
} | |
} |
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
'use strict'; | |
var WebSocketServer = require('ws').Server | |
, http = require('http') | |
, path = require('path') | |
, fs = require('fs') | |
, server | |
, wss; | |
function noop(err) { | |
if (err) console.error(err.stack); | |
} | |
wss = new WebSocketServer({ clientTracking: false, noServer: true }); | |
server = http.createServer(); | |
server.on('upgrade', function (req, socket, head) { | |
wss.handleUpgrade(req, socket, head, function (socket) { | |
console.log('open'); | |
socket.on('message', function (data) { | |
if ('shutdown' === data) { | |
socket.send('whatever', noop); | |
socket.close(); | |
} | |
}); | |
socket.on('error', function (err) { | |
console.error(err.stack || err); | |
}); | |
socket.on('close', function (code, reason) { | |
console.log('close, code: %s, reason: %s', code, reason); | |
}); | |
}); | |
}); | |
server.on('request', function (req, res) { | |
if (req.url === '/client.js') { | |
res.writeHead(200, { 'Content-Type': 'application/javascript' }); | |
fs.createReadStream(path.join(__dirname, '/client.js')).pipe(res); | |
return; | |
} | |
res.writeHead(200, { 'Content-Type': 'text/html' }); | |
fs.createReadStream(path.join(__dirname, '/index.html')).pipe(res); | |
}); | |
server.listen(3000, function () { | |
console.log('listening on port 3000'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment