この記事でmikeが書いたnode.jsのマルチルームチャットアプリケーションの例を使用し、今までphpセッションハンドラーから取得したセッションデータを使用するように変更しました
これは私が今まで書いたコードの一部です
var express = require('express'),
app = express(),
memcache = require("memcache"),
http = require('http'),
server = http.createServer(app),
io = require('socket.io').listen(server),
co = require("./cookie.js"),
php = require('phpjs'),
codein = require("node-codein");
//check if user loggedin
// routing
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
var cookieManager = new co.cookie(req.headers.cookie);
var client = new memcache.Client(11211, "localhost");
client.connect();
user = client.get("sessions/"+cookieManager.get("sec_session_id"), function(error, result){
var session = JSON.parse(result);
user = JSON.parse(session.name);
user = user.username;
storeUsername(user);
});
});
function storeUsername(user){
// usernames which are currently connected to the chat
var usernames = {};
io.of('/private').authorization(function (handshakeData, callback) {
console.dir(handshakeData);
handshakeData.foo = 'baz';
callback(null, true);
}).io.sockets.on('connection', function (socket) {
usernames[socket.id] = socket;
// when the client emits 'sendchat', this listens and executes
socket.on('sendchat', function (data) {
// we tell the client to execute 'updatechat' with 2 parameters
io.sockets.emit('updatechat', socket.username, data);
});
// when the client emits 'adduser', this listens and executes
socket.on('adduser', function(username){
// we store the username in the socket session for this client
socket.username = user;
// add the client's username to the global list
// echo to client they've connected
if(php.in_array(socket.username,usernames)){
delete usernames[socket.username];
}else{
usernames[user] = user;
console.log('not exist');
socket.emit('updatechat', 'SERVER', 'you have connected');
// echo globally (all clients) that a person has connected
socket.broadcast.emit('updatechat', 'SERVER', username + ' has connected');
// update the list of users in chat, client-side
io.sockets.emit('updateusers', usernames);
}
});
// when the user disconnects.. perform this
socket.on('disconnect', function(){
// remove the username from global usernames list
delete usernames[socket.username];
// update list of users in chat, client-side
io.sockets.emit('updateusers', usernames);
// echo globally that this client has left
socket.broadcast.emit('updatechat', 'SERVER', socket.username + ' has disconnected');
});
});
}
server.listen(3000);
たとえば、ユーザーマスターは私たちのチャットルームに接続し、彼はphpベースのアプリケーションから保存されたユーザー名を持っています.しかし、今どこに問題がありますか?ユーザーマスターがブラウザの2または3タブから接続すると、彼はソケットサーバー3または4に接続します回と彼がいくつかのデータを投稿すると、この結果が得られます
master : hello
master : hello
master : hello
ユーザーが自分のアプリケーションに一度だけ接続し、データを一度だけ投稿できるようにしたいのですが、どうすればそれを達成できますか?
お互いにプライベート メッセージを送信する場合、このユーザーにアクセスするにはどうすればよいですか
私はnode.js.my bad.sorryでとても新しいです
事前に助けてくれてありがとう。すべての教師に+1