0
페이스 북 친구가 서로 만 이야기 할 수있는 채팅을하고 있습니다. 나는 redis를 사용하여 관계를 저장합니다 : fb_user_id - user_socket_id. 내 구현은 다음과 같습니다.facebook 친구들과 node.js 및 socket.io 채팅
- facebook으로부터 친구를 사귀기;
- 내 친구의 소켓 ID를 redis에서 선택하고 내 노드 클라이언트에 로컬 친구 소켓 목록을 만듭니다.
- 노드 서버에 연결 중입니다. 서버가 내 소켓 ID를 redis에 저장하고 모든 친구에게 새 친구 로그인에 대해 알립니다 (나에 관해서).
- 모든 친구가 로컬 친구 소켓 목록을 업데이트합니다.
- 누군가가 서버에 채팅 메시지를 보낼 때이 메시지는 친구 - 소켓 목록과 함께 제공되므로 서버는 메시지를 보낼 위치 (내 친구 만 해당)를 알 수 있습니다.
질문 : 언제든지 서버에 친구 소켓을 보내거나 서버에서이 관계를 서버에서 가져 오거나 서버에 소켓 배열을 만드는 것이 더 좋습니다. 고 가용성을 위해 내 작업을 조정하는 방법은 무엇입니까?
모든 의견과 제안을 환영합니다. 감사합니다. 여기
이 봐, 내 (1.2.0 socket.io) 코드server.js
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var redis = require("redis"), client = redis.createClient();
var parts;
client.select(2, function() {});
client.on("error", function (err) {
console.log("Error " + err);
});
process.on('uncaughtException', function (err) {
console.log(err);
});
app.get('/', function (req, res) {
res.sendFile(__dirname + '/index.html');
});
io.on('connection', function (socket) {
// on connect
socket.on("join", function (data)
{
if (data) {
// notify all friedns about new friend login
if (data.to) {
if (data.to.length > 0) {
for (x in data.to) {
io.to(data.to[x]['socket_id']).emit('new friend response', {uid: data.uid, sid: socket.id});
}
}
}
// save or update user socket id to redis
parts = split_id(data.uid);
client.hset(parts[1], parts[0], socket.id);
}
});
// disconnect
socket.on('disconnect', function() {
console.log("user disconnected");
});
// send message by friends-sockets list
socket.on('chat message', function (data) {
if (data.to.length > 0) {
for (x in data.to) {
var message = data.msg;
io.to(data.to[x]['socket_id']).emit('chat message response', {msg: message, uid: data.uid});
}
}
});
});
http.listen(3000, function() {
console.log('listening on *:3000');
});
// split facebook uid in 2 parts (for redis saving)
function split_id(str)
{
var n = str.length;
var res1 = str.substr(n - 2, 2);
var res2 = str.substr(0, n - 2);
return [res1, res2];
}
client.js
// friends socket list
var friends_sockets = [];
// my data from facebook
var my_data;
// my facebook uid
var my_uid;
function client() {
socket = io('http://server.com:3000');
// connect
socket.on('connect', function() {
// notify server about login
socket.emit('join', {uid: my_uid, to: friends_sockets, from: my_data, type: 'web'});
});
// send chat message to my friends
$('.enter_form button').click(function() {
if (friends_sockets.length > 0) {
socket.emit('chat message', {msg: $('#m').val(), to: friends_sockets, from: my_data, uid: my_uid});
}
// add message to my chat
$('#messages').append($('<li>').text(my_data.first_name + ' ' + my_data.last_name + ': ' + $('#m').val()));
$('#m').val('');
return false;
});
// new message listner (waiting for chat messages)
socket.on('chat message response', function (data) {
$('#messages').append($('<li>').text(data.msg));
});
// new friends lister (update list on friends login)
socket.on('new friend response', function (data) {
var found = false;
if (friends_sockets.length > 0) {
for (x in friends_sockets) {
if (friends_sockets[x]['uid'] == data.uid) {
friends_sockets[x]['socket_id'] = data.sid;
found = true;
}
}
}
if (found === false) {
friends_sockets.push(data);
}
});
}