记录一个最简单的nodejs两个端口相互转发的案例,只局限于1对1
记录一个最简单的nodejs两个端口相互转发的案例,只局限于1对1,后期如果有需要可以做成多个端口转发,有需要可以做成聊天室
const net = require('net'); var data01; var data02; net.createServer().on('connection',(data)=>{ console.log("client 01 connected"); data01 = data; try{data.on('data',(chunk)=>{data02.write(chunk);});}catch(e){} data.on('close',(p1)=>{data01=null;console.log("client 01 closed");}); data.on('error',(p1)=>{data01=null;console.log("client 01 error");}); }).listen(5678); net.createServer().on('connection',(data)=>{ console.log("client 02 connected"); data02 = data; try{data.on('data',(chunk)=>{data01.write(chunk);});}catch(e){} data.on('close',(p1)=>{data02=null;console.log("client 02 closed");}); data.on('error',(p1)=>{data02=null;console.log("client 02 error");}); }).listen(5679)
参考链接 https://www.cnblogs.com/lguow/p/11718359.html
其他一些废代码也贴下来,仅供参考
/* const express = require('express'); const http = require('http'); const app8000 = express(); const app9000 = express(); const server8000 = http.Server(app8000); const server9000 = http.Server(app9000); const io8000 = require('socket.io')(server8000); const io9000 = require('socket.io')(server9000); server8000.listen(5678); server9000.listen(5679); //端口8000上的服务器 io8000.on('connection',socket => { socket.on('my-event',data => { //将数据推送到端口9000上连接到服务器的客户端 console.log("io1"); io9000.emit(" event-from-8000"," From 8000:" + data); }); }); //端口9000上的服务器 io9000.on('connection',socket => { socket.on('my-event',data => { console.log("io2"); //将数据推送到端口8000上连接到服务器的客户端 io8000.emit(" event-from-9000"," From 9000:" + data); }); }); */ /* var http1 = require('http'); var http2 = require('http'); var fs = require('fs'); var url = require('url'); // 创建服务器 http1.createServer( function (request, response) { response.write(data.toString()); response.end(); }); }).listen(8080); // 控制台会输出以下信息 console.log('Server running at http://127.0.0.1:8080/'); */ /* //这个是客户端 var WebSocket = require('ws'); const ws1 = new WebSocket('ws://121.36.38.208:5678'); const ws2 = new WebSocket('ws://121.36.38.208:5679'); ws1.onmessage = (msg)=>{ws2.send(msg)}; ws2.onmessage = (msg)=>{ws1.send(msg)}; //服务端 var ws = require("nodejs-websocket"); var server = ws.createServer(function (conn) { console.log("New connection") conn.on("text", function (str) { console.log("Received "+str) conn.sendText(str.toUpperCase()+"!!!") }) conn.on("close", function (code, reason) { console.log("Connection closed") }) conn.on("error",function(err){ console.log('handler error') console.log(err) }) }).listen(8080) */ /* const net = require('net'); const server01 = net.createServer(); var data01; var data02; server01.on('connection',(data)=>{ console.log("client 01"); data01 = data; try{ data.on('data',(chunk)=>{ data02.write(chunk); }); }catch(e){} }) server01.listen(5678); const server02 = net.createServer(); server02.on('connection',(data)=>{ console.log("client 01"); data02 = data; //data.setEncoding('utf8'); // 客户socket进程绑定事件 try{ data.on('data',(chunk)=>{ data01.write(chunk); }); }catch(e){} }) server02.listen(5679); */ //综合比较这个最简单 需要模块 npm install net const net = require('net'); var data01; var data02; net.createServer().on('connection',(data)=>{ console.log("client 01 connected"); data01 = data; try{data.on('data',(chunk)=>{data02.write(chunk);});}catch(e){} }).listen(5678); net.createServer().on('connection',(data)=>{ console.log("client 02 connected"); data02 = data; try{data.on('data',(chunk)=>{data01.write(chunk);});}catch(e){} }).listen(5679)