Exemplo n.º 1
0
 connection.on('close', function(reasonCode, description) {
       for(var i in clients)
            if(clients[i].nickname==connection.nickname)
                 clients.splice(i,1);
       jsonobj.type="userleave";
       jsonobj. content=connection.nickname;
       wsServer.bcast(jsonobj);
       console.log((new Date()).toLocaleTimeString() +" "+ connection.nickname/*.remoteAddress*/ + ' disconnected.');
 });
Exemplo n.º 2
0
    connection.on('message', function(message) {
        if (message.type === 'utf8') {
            //console.log('Received Message: ' + message.utf8Data);
            //connection.sendUTF(message.utf8Data);
		jsonobj=JSON.parse(message.utf8Data);
		if(jsonobj.type=="regist")
		{
			if(!islegal(jsonobj.content))
			{
				jsonobj.content="failed";
				connection.sendUTF(JSON.stringify(jsonobj));
			}
			else
			{
				connection.nickname=jsonobj.content;
				wsServer.bcast(jsonobj);
				connection.sendUTF(JSON.stringify(jsonobj));
				for(var i in clients)
				{
					jsonobj.content=clients[i].nickname;
					connection.sendUTF(JSON.stringify(jsonobj));
				}
				clients.push(connection);
			}
		}
		else if(jsonobj.type=="public")
		{
			wsServer.bcast(jsonobj);
			console.log(jsonobj.sender+" broadcasting message:"+jsonobj.content);
		}
		else
		{
		     for(var i in clients)
		          if(clients[i].nickname==jsonobj.type)
		          {
		               clients[i].sendUTF(JSON.stringify(jsonobj));
		               connection.sendUTF(JSON.stringify(jsonobj));
		               console.log(jsonobj.sender+" send "+jsonobj.type+" private message:"+jsonobj.content);
		               break;
		           }
		}			
        }
    });