Node.js와 WebSocket을 사용하여 기본 일대일 채팅을 만들고 있습니다. 클라이언트가 연결할 때마다 ID와 salt + id의 MD5 해시가 전송됩니다. 그런 다음 다른 클라이언트와 쌍을 이루어야합니다. 쌍을 이루면 salt + partnerid의 ID와 MD5 해시가 전송됩니다. 메시지가 전송 될 때마다 해시가 검사됩니다. 이는 Javascript ID 변수의 값을 변경하고 메시지를 다시 라우팅 할 수 없도록하기위한 것입니다.쌍 웹 소켓 클라이언트
[부분] server.js
var salt = "kPNtvp2UoBQRBcJ";
var count = 0;
var clients = {};
wsServer.on('request', function(r){
var connection = r.accept('echo-protocol', r.origin);
var id = count++;
clients[id] = connection;
console.log((new Date()) + ' Connection accepted [' + id + ']');
clients[id].sendUTF(JSON.stringify({"type": "id", "id": id, "hash": md5(salt+id)}));
connection.on('message', function(message) {
var data = JSON.parse(message.utf8Data);
console.log((new Date()) + ' New ' + data.type + ' sent from ' + data.from.id + ' to ' + data.to.id + ': ' + data.message);
if(checkHash(data.from.id, data.from.hash) && checkHash(data.to.id, data.to.hash)){
clients[data.to.id].sendUTF(message.utf8Data);
clients[data.from.id].sendUTF(message.utf8Data);
}else{
console.log((new Date()) + ' Client hashes invalid, alerting sender and intended recipient.');
clients[data.from.id].sendUTF(JSON.stringify({"type": "message", "message": "Our system has detected that you attempted to reroute your message by modifying the Javascript variables. This is not allowed, and subsequent attempts may result in a ban. The user you attempted to contact has also been notified.", "from": {"id": "system", "hash": ""}, "to": {"id": data.to.id, "hash": ""}}));
clients[data.to.id].sendUTF(JSON.stringify({"type": "message", "message": "Someone you are not chatting with just attempted to send you a message by exploiting our system, however we detected it and blocked the message. If you recieve any subsequent messages that seem unusual, please be sure to report them.", "from": {"id": "system", "hash": ""}, "to": {"id": data.to.id, "hash": ""}}));
}
});
connection.on('close', function(reasonCode, description) {
delete clients[id];
console.log((new Date()) + ' Peer ' + connection.remoteAddress + ' disconnected.');
});
});
이제이 모두 잘 작동하지만 내 문제는 클라이언트를 페어링되어. 페어링하기 위해, 메시지는 다음과 같습니다 두 클라이언트로 전송되어야한다 : 나는 파트너를 찾는 생각
{"type": "partner", "id": PARTNERID, "hash": md5(sald+id)}
한 가지 방법은 그들이 파트너를했다 묻는 모든 클라이언트에 메시지를 보낼 것입니다, 그리고 나서 false
이라고 대답 한 것들을 매치시키면서 클라이언트 측 서버를 추적하는 것이 더 쉬울 것이라고 생각합니다. 어느 것을해야합니까? 코드가 어떻게 생깁니 까?
당신이 제안한 방법대로했습니다. 누구든지 코드를 보는 데 관심이 있다면 https://github.com/OvalBit/Sigma의 소스 코드 –
위대한 직장 동료입니다. node.js와 함께 일한 적은 일반적인 포인트에서 대답했습니다. – xvidun