Last active
January 18, 2017 17:44
-
-
Save tdhsmith/8e71aba02c684f549cdec05761e6e87b to your computer and use it in GitHub Desktop.
websocket-monitor opcode test server
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
const http = require('http'); | |
const WebSocket = require('ws'); | |
// you'll need to npm install ws (tested on v1.1.1, which was the default as of this writing) | |
const wss = new WebSocket.Server({ | |
port: 3001 | |
}); | |
wss.on('error', console.error); | |
wss.on('listening', () => console.log("* WS server ready")); | |
wss.on('connection', function connection(ws) { | |
console.log("* client WS connected\n"); | |
let messageCounter = 0; | |
ws.on('message', function incoming(data, flags) { | |
if (flags.binary) { | |
console.log( | |
"-> BINARY (0x2); " | |
+ (flags.masked ? "MASKED" : "unmasked") | |
); | |
} else { | |
console.log( | |
"-> TEXT (0x1); " | |
+ (flags.masked ? "MASKED" : "unmasked") | |
); | |
} | |
if (++messageCounter == 2) | |
{ | |
beginServerTest(ws); | |
} | |
}); | |
ws.on('ping', function incoming(data, flags) { | |
console.log( | |
"-> PING (0x9); " | |
+ (flags.masked ? "MASKED" : "unmasked") | |
); | |
}); | |
ws.on('pong', function incoming(data, flags) { | |
console.log( | |
"-> PONG (0xA); " | |
+ (flags.masked ? "MASKED" : "unmasked") | |
); | |
}); | |
ws.on('close', function close(code, message) { | |
console.log("-> CLOSE (0x8)"); | |
process.exit(); | |
}) | |
}); | |
function beginServerTest(ws) { | |
var i = 0; | |
function multipart (err, send) | |
{ | |
if (err) return console.error(err); | |
if (i % 2 == 0) { | |
send("start of text...", false); | |
} else { | |
send("...end of text", true); | |
process.nextTick(() => { | |
ws.close(1000, "done"); | |
console.info("<- CLOSE (0x8)"); | |
}); | |
} | |
i++; | |
} | |
ws.send(Buffer.from("61626364", "hex"), {binary: true}); | |
console.info("<- BINARY (0x2); unmasked"); | |
ws.ping("PING"); | |
console.info("<- PING (0x9); unmasked"); | |
ws.pong("PONG"); | |
console.info("<- PONG (0xA); unmasked"); | |
ws.stream(multipart); | |
console.info("<- TEXT (0x1); unmasked"); | |
console.info("<- CONTINUATION (0x0); unmasked"); | |
} | |
const clientContent = ` | |
<html> | |
<head><title>WS test</title></head> | |
<body> | |
<h1>Testing websockets</h1> | |
<script type="text/javascript"> | |
const ws = new WebSocket('ws://localhost:3001'); | |
ws.onopen = () => { | |
console.log("ws open!"); | |
ws.send("text frame"); | |
ws.send(new Uint8Array([1, 2, 3])); | |
} | |
ws.onerror = console.error; | |
ws.onclose = console.error; | |
</script> | |
</body> | |
</html> | |
` | |
http.createServer(function(request, response) { | |
response.writeHead(200, {"Content-Type": "text/html; charset=utf-8"}); | |
response.write(clientContent, "utf-8"); | |
response.end(); | |
console.log("* served static html"); | |
}).listen(3000); | |
console.log("* server started"); |
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
� M<+ �������� / Mac OS X 10.11.6, build 15G1212 (Darwin 15.6.0) - Dumpcap (Wireshark) 2.2.3 (v2.2.3-0-g57531cd) � d lo0 tcp / Mac OS X 10.11.6, build 15G1212 (Darwin 15.6.0) d d aF {��A A E =v?@ @ ͜#R�_q��4x� �1 | |
d��� � d X aF D{��8 8 E 4ܰ@ @ #R͜�4x�_q��)��( | |
d� d�X d aF E{��A A E =�@ @ ͝#R-��ױ�r(�-S�1 | |
d��� L d X aF U{��8 8 E 4�@ @ #R͝��r(-����*��( | |
d� d�X � aF �{��u u E q�@ @ #R͜�4x�_q��)��e | |
d� d�� � 4 COMPRESSION snappy lz4 CQL_VERSION 3.4.2 � � aF �{��u u E q�@ @ #R͝��r(-����*��e | |
d� d�� L 4 COMPRESSION snappy lz4 CQL_VERSION 3.4.2 � X aF |��8 8 E 4${@ @ ͜#R�_q��4�� �( | |
d� d�X X aF |��8 8 E 4�@ @ ͝#R-��౬re�-Q�( | |
d� d�X d aF �D��A A E =�@ @ ͜#R�_q��4�� �1 | |
�g d� � d d aF E��A A E =�@ @ ͝#R-��౬re�-Q�1 | |
�g d� M d X aF E��8 8 E 4�!@ @ #R͜�4��_q��)��( | |
�g �gX X aF (E��8 8 E 44@ @ #R͝��re-����*��( | |
�g �gX � aF �E��u u E q��@ @ #R͝��re-����*��e | |
�g �g� M 4 COMPRESSION snappy lz4 CQL_VERSION 3.4.2 � � aF �E��u u E q��@ @ #R͜�4��_q��)��e | |
�g �g� � 4 COMPRESSION snappy lz4 CQL_VERSION 3.4.2 � X aF �E��8 8 E 4֥@ @ ͝#R-��鱬r��-O�( | |
�g �gX X aF �E��8 8 E 4_A@ @ ͜#R�_q��4�� �( | |
�g �gX d aF z���D D E @�@ @ ���Ki��� ����4 ?� | |
!: d d aF ����D D E @�@ @ �K���I�Oi�溰���4 ?� | |
!:!: d X aF ����8 8 E 4a�@ @ ���Ki����I�P�1��( | |
!:!:X X aF ɛ��8 8 E 4��@ @ �K���I�Pi�満1��( | |
!:!:X ` aF ���? ? E ; | |
�@ @ ���Ki����I�P�1��/ | |
!:!:JRMI K ` X aF *���8 8 E 4B@ @ �K���I�Pi����1��( | |
!:!:X h aF ����H H E D �@ @ �K���I�Pi����1��8 | |
!:!:N 127.0.0.1 ��h X aF ���8 8 E 4-f@ @ ���Ki����I�`�1��( | |
!:!:X h aF (���G G E C��@ @ ���Ki����I�`�1��7 | |
!:!: 127.0.0.1 h X aF T���8 8 E 4b3@ @ �K���I�`i��Ѐ1��( | |
!:!:X � aF `��٢ � E ��@ @ ���Ki����I�`�1��� | |
!:!:P�� w" |