Created
June 30, 2014 07:54
-
-
Save bodokaiser/f226307564e9cacd5925 to your computer and use it in GitHub Desktop.
WebSocket benchmark setup between walve and ws.
This file contains 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> | |
<title>WebSocket Benchmark</title> | |
</head> | |
<body> | |
<h1>WS Bench</h1> | |
<form> | |
<input placeholder="Type a message or drag file." /> | |
</form> | |
<script> | |
var wsocket = new WebSocket('ws://localhost:3000'); | |
wsocket.onopen = function() { | |
var start; | |
document.querySelector('form').onsubmit = function(e) { | |
e.preventDefault(e); | |
start = performance.now(); | |
wsocket.send(e.target.value); | |
}; | |
document.querySelector('input').ondrop = function(e) { | |
e.preventDefault(); | |
e.stopPropagation(); | |
[].slice.call(e.dataTransfer.files).forEach(function(file) { | |
if (!file.type.match(/image.*/)) return; | |
var reader = new FileReader(); | |
reader.onload = function(e) { | |
start = performance.now(); | |
wsocket.send(e.target.result); | |
}; | |
reader.readAsDataURL(file); | |
}); | |
}; | |
wsocket.onmessage = function() { | |
alert('Echoed back in ' + (performance.now() - start) + 'ms'); | |
}; | |
wsocket.onclose = function() { | |
alert('Connection closed!'); | |
}; | |
}; | |
</script> | |
</body> | |
</html> |
This file contains 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 fs = require('fs'); | |
var http = require('http'); | |
var walve = require('walve'); | |
var server = http.createServer(function(req, res) { | |
fs.createReadStream(__dirname + '/index.html').pipe(res); | |
}).listen(3000); | |
var wsserver = walve.createServer(function(wsocket) { | |
wsocket.on('incoming', function(incoming) { | |
var outgoing = new walve.Outgoing({ | |
header: { | |
final: incoming.header.final, | |
opcode: incoming.header.opcode, | |
length: incoming.header.length | |
} | |
}); | |
incoming.pipe(outgoing).pipe(wsocket, { end: false }); | |
}); | |
}).listen(server); |
This file contains 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 fs = require('fs'); | |
var ws = require('ws'); | |
var http = require('http'); | |
var server = http.createServer(function(req, res) { | |
fs.createReadStream(__dirname + '/index.html').pipe(res); | |
}).listen(3000); | |
var wsserver = new ws.Server({ server: server }); | |
wsserver.on('connection', function(wsocket) { | |
wsocket.on('message', function(message) { | |
wsocket.send(message); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment