2013-01-02 42 views
26

Máy chủ có thể kết nối với máy chủ khác bằng Socket.IO và được xử lý như một máy khách không?Máy chủ IO ổ cắm vào máy chủ

Và hãy tham gia phòng, nhận io.sockets.in ('lobby'). Emit(). Và hơn thế nữa?

Máy chủ đầu tiên cũng đang nghe các kết nối/tin nhắn.

Hey Brad, đây là đầy đủ .js ứng dụng của tôi dưới đây để tham khảo:

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

Bản sao có thể có của [ứng dụng Node.js cho máy chủ socket.io] (https://stackoverflow.com/questions/10703513/node-js-client-for-a-socket-io-server) – StefansArya

Trả lời

35

Vâng, hoàn toàn. Chỉ cần sử dụng ứng dụng Socket.IO trong ứng dụng máy chủ của bạn trực tiếp.

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

Bạn có thể cài đặt nó với npm install socket.io-client. Sau đó, để sử dụng:

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

Hey Brad, nó sẽ có thể nhận được một tin nhắn socket.on ('tin nhắn'), và sau đó chuyển tiếp tin nhắn đến máy chủ thứ hai bằng cách sử dụng Socket IO? Cảm ơn, -T – Taurian

+0

Có, bạn có thể sử dụng phía máy chủ khách hàng Socket.IO giống như cách bạn sử dụng nó phía máy khách. Bạn có thể làm bất cứ điều gì bạn muốn với các tin nhắn. – Brad

+0

Ok tuyệt vời, tôi đưa ứng dụng của mình lên, dễ dàng sao chép/dán và chạy. Bạn có thể giúp tôi điền vào chỗ trống không? Làm thế nào để kết nối với máy chủ thứ hai đi vào chơi? Cảm ơn, -T – Taurian

14

Tôi nhận ra đây là một bài cũ, nhưng tôi đã làm việc trên một cái gì đó tương tự và đã quyết định quay trở lại và đóng góp một cái gì đó vì nó đã cho tôi suy nghĩ ..

Dưới đây là một khách hàng cơ bản - > server 1 -> server 2 thiết lập

server # 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 
    }); 
}); 

Và đây là server # 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... 
    }); 
}); 

Đây là khách hàng của chúng tôi, người gửi thư gốc.

// 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); 
    }); 
}); 

Tôi đang tạo bài đăng này là Wiki cộng đồng để ai đó có thể cải thiện điều này nếu họ cảm thấy thích.

!! MÃ KHÔNG ĐƯỢC KIỂM TRA, SỬ DỤNG RỦI RO RIÊNG CỦA BẠN !!

+0

Người đàn ông đẹp! Điều này đã cho tôi một tên lửa cho một thiết lập cluster.io cluster. Cảm ơn nhiều. –

+0

Thực sự đã giúp tôi, cảm ơn! – FirstLegion

Các vấn đề liên quan