2013-01-02 15 views
26

क्या सर्वर के लिए सॉकेट.आईओ का उपयोग करके दूसरे से जुड़ना संभव है और क्लाइंट की तरह व्यवहार किया जा सकता है?सॉकेट आईओ सर्वर सर्वर

और इसे कमरे में शामिल करें, io.sockets.in ('लॉबी') प्राप्त करें। Emit()। और अधिक?

पहला सर्वर कनेक्शन/संदेशों के साथ भी सुन रहा है।

अरे ब्राड, यहाँ मेरा पूरा .js एप्लिकेशन संदर्भ के लिए नीचे दिया गया है:

var io = require("socket.io").listen(8099); 
io.set('log level', 1); 

io.sockets.on("connection", function (socket) { 

    console.log('A Client has Connected to this Server'); 

    //Let Everyone Know I just Joined 
    socket.broadcast.to('lobby').emit("message",'UC,' + socket.id); // Send to everyone in Room but NOT me 


socket.on("message", function (data) { 

//Missing code 
socket2.send('message,' + data); //Forward Message to Second Server 

}); 

socket.on("disconnect", function (data) { 
    //Send Notification to Second Server 
    //Need to figure out later 

    //Send Notification to Everyone 
    socket.broadcast.emit("message",'UD,' + socket.id); //Send to Everyone but NOT me 

    //Remove user from Session ID 
    arSessionIDs.removeByValue(socket.id);  

    //Send Notification to Console 
    console.log("disconnecting " + arRoster[socket.id][1]); 
}); 

}); 

var io_client = require('socket.io-client'); 
var socket2 = io_client.connect('http://192.168.0.104:8090'); 
socket2.on('connect', function() { 
socket2.emit('C3434M,Test'); 
}); 
+0

[एक socket.io सर्वर के लिए Node.js ग्राहक] (https://stackoverflow.com/questions/10703513/node-js-client-for-a-socket-io-server) – StefansArya

उत्तर

35

हाँ, बिल्कुल। बस अपने सर्वर एप्लिकेशन में Socket.IO क्लाइंट का उपयोग करें।

https://github.com/LearnBoost/socket.io-client

आप npm install socket.io-client साथ स्थापित कर सकते हैं। तब उपयोग करने के लिए:

var socket = io.connect('http://example.com'); 
socket.on('connect', function() { 
    // socket connected 
    socket.emit('server custom event', { my: 'data' }); 
}); 
+0

की संभावित डुप्लिकेट अरे ब्रैड, क्या सॉकेट IO ('message') संदेश प्राप्त करना संभव होगा, और फिर उस संदेश को सॉकेट IO का उपयोग करके दूसरे सर्वर पर अग्रेषित करें? धन्यवाद, -टी – Taurian

+0

हां, आप सॉकेट.आईओ क्लाइंट सर्वर-साइड का उपयोग उसी तरह कर सकते हैं जैसे आप क्लाइंट-साइड का उपयोग करते हैं। आप संदेश के साथ जो कुछ भी चाहते हैं वह कर सकते हैं। – Brad

+0

ठीक है, मैंने अपना ऐप रखा, कॉपी/पेस्ट करने और चलाने में आसान। क्या आप मुझे रिक्त स्थान भरने में मदद कर सकते हैं? दूसरे सर्वर से कनेक्ट कैसे खेलता है? धन्यवाद, -टी – Taurian

14

मुझे पता है यह एक पुरानी पोस्ट है, लेकिन मैं कुछ इसी तरह पर काम कर रहा था और वापस आने और कुछ योगदान के रूप में यह मुझे सोच मिला करने का फैसला किया ..

यहाँ एक बुनियादी क्लाइंट है - > सर्वर 1 -> सर्वर 2 सेटअप

सर्वर # 1

// Server 1 
var io = require("socket.io").listen(8099); // This is the Server for SERVER 1 
var other_server = require("socket.io-client")('http://domain.com:8100'); // This is a client connecting to the SERVER 2 

other_server.on("connect",function(){ 
    other_server.on('message',function(data){ 
     // We received a message from Server 2 
     // We are going to forward/broadcast that message to the "Lobby" room 
     io.to('lobby').emit('message',data); 
    }); 
}); 

io.sockets.on("connection",function(socket){ 
    // Display a connected message 
    console.log("User-Client Connected!"); 

    // Lets force this connection into the lobby room. 
    socket.join('lobby'); 

    // Some roster/user management logic to track them 
    // This would be upto you to add :) 

    // When we receive a message... 
    socket.on("message",function(data){ 
     // We need to just forward this message to our other guy 
     // We are literally just forwarding the whole data packet 
     other_server.emit("message",data); 
    }); 

    socket.on("disconnect",function(data){ 
     // We need to notify Server 2 that the client has disconnected 
     other_server.emit("message","UD,"+socket.id); 

     // Other logic you may or may not want 
     // Your other disconnect code here 
    }); 
}); 

और यहाँ है सर्वर # 2

// Server 2 
var io = require("socket.io").listen(8100); 
io.sockets.on("connection",function(socket){ 
    // Display a connected message 
    console.log("Server-Client Connected!"); 

    // When we receive a message... 
    socket.on("message",function(data){ 
     // We got a message... I dunno what we should do with this... 
    }); 
}); 

यह हमारा ग्राहक है, जो मूल संदेश भेजता है।

// Client 
var socket = io('http://localhost'); 
socket.on('connect', function(){ 
    socket.emit("message","This is my message"); 

    socket.on('message',function(data){ 
     console.log("We got a message: ",data); 
    }); 
}); 

मैं इस पोस्ट को एक समुदाय विकी बना रहा हूं ताकि अगर कोई ऐसा महसूस कर सके तो कोई इसे बेहतर बना सकता है।

!! कोड को परीक्षण नहीं किया गया है, अपने जोखिम पर उपयोग करें !!

+0

सुंदर आदमी! यह मुझे एक सॉकेट.ओ क्लस्टर सेटअप के लिए रॉकेटशिप मिला। आपका बहुत बहुत धन्यवाद। –

+0

वास्तव में मेरी मदद की, धन्यवाद! – FirstLegion

संबंधित मुद्दे