Created
July 24, 2018 09:45
-
-
Save jamo/86c54d95e7e0d945becc8d0b57d7cf1b to your computer and use it in GitHub Desktop.
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 WebSocket = require('ws'); | |
const ws = new WebSocket(`wss://${process.argv[2]}/`, { | |
origin: `https://${process.argv[2]}` | |
}); | |
ws.on('open', function open() { | |
console.log('connected'); | |
ws.send(Date.now()); | |
}); | |
ws.on('close', function close() { | |
console.log('disconnected'); | |
}); | |
ws.on('message', function incoming(data) { | |
console.log(`ping`); | |
setTimeout(function timeout() { | |
ws.send(Date.now()); | |
}, 500); | |
}); | |
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 WebSocket = require('ws'); | |
const http = require('http'); | |
const server = http.createServer((req, res) => { | |
console.log(req.headers) | |
res.writeHead(200, { 'Content-Type': 'text/plain' }); | |
res.end('okay'); | |
}); | |
http.createServer(); | |
const wss = new WebSocket.Server({ server }); | |
wss.on('connection', function connection(ws, req) { | |
console.log('connection') | |
console.dir(req.headers) | |
ws.on('message', function incoming(message) { | |
console.log('received: %s', message); | |
}); | |
ws.send('something'); | |
}); | |
server.listen(3000) |
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": "ws-testing", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"start": "node index.js", | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"keywords": [], | |
"author": "Jarmo Isotalo <[email protected]>", | |
"license": "MIT", | |
"dependencies": { | |
"ws": "6.0.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment