1. 程式人生 > 實用技巧 >記錄一個最簡單的nodejs兩個埠相互轉發的案例,只侷限於1對1

記錄一個最簡單的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)