Skip to content

Instantly share code, notes, and snippets.

@Ran-ying
Created January 30, 2023 20:27
Show Gist options
  • Save Ran-ying/ed4b49bccbcb5433f48bbba8348b6e96 to your computer and use it in GitHub Desktop.
Save Ran-ying/ed4b49bccbcb5433f48bbba8348b6e96 to your computer and use it in GitHub Desktop.

WS 服务端

const WebSocketServer = require('ws').Server;
const wss =new WebSocketServer({
    ip: "127.0.0.1",
    port:4000
})
wss.on('connection', (ws)=>{
    ws.on('message',(message) => {
        ws.send(message)
    })
})

TCP 服务端

require("net").createServer(con =>{
    con.on('data',(data)=>{
    })
}).listen(3000)

TCP 客户端

var client = require("net").connect('3000','127.0.0.1');
client.on('data',(message)=>{
    client.write(message);
})

HTTP 服务端

require('http').createServer((req,res)=>{
    console.log(req.headers);
    res.setHeader('Content-Type',"text/html");
    res.end(req.url);
}).listen(4000);

HTTP 客户端

require('http').request({
    host: '127.0.0.1',
    url: '/',
},(res)=>{
    res.on('data',(data)=>{
        console.log(data);
    })
    res.on('end',()=>{
        console.log("End");
    })
}).end();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment