Created
June 15, 2018 02:45
-
-
Save yuki-takeichi/9c18a30e7e35e558a4299baeb87508e9 to your computer and use it in GitHub Desktop.
socket.js
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
const net = require('net'); | |
const server = net.createServer(c => { | |
c.setEncoding('ascii'); | |
console.log('client connected'); | |
c.on('end', () => { | |
console.log('client disconnected'); | |
}); | |
c.on('data', data => { | |
//console.log(Buffer.from(data, 'ascii')); | |
console.log(data); | |
if (data.includes("\r\n\r\n")) { | |
console.log('hogehoge'); | |
const resp = | |
"HTTP/1.1 200 OK\r\n" + | |
"Content-Type: text/html; charset=utf-8\r\n" + | |
"Content-Length: 4\r\n" + | |
"Date: Thu, 24 May 2018 04:35:37 GMT\r\n" + | |
"Connection: keep-alive\r\n" + | |
"\r\n" + | |
"pong"; | |
c.write(resp); | |
} | |
}); | |
}); | |
server.on('error', (err) => { | |
throw err; | |
}); | |
server.listen(3000, () => { | |
console.log('server bound'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment