我有一个带有不同事件的nodejs聊天服务器 “连接”,“sendchat”,“switchRoom”......
///////////////////////////////////////////////////////////////
// SERVER SETUP
var express = require('express')
, app = express()
, http = require('http')
, server = http.createServer(app)
, io = require('socket.io').listen(server);
server.listen(1214);
var admin_name = '<font color="red">*Admin*</font>';
var users;
// rooms which are currently available in chat
var rooms = ['public'];
io.sockets.on('connection', function (socket) {
// when the client emits 'adduser', this listens and executes
socket.on('adduser', function(username, password){
getLogin(username, password, socket)
});
// when the client emits 'sendchat', this listens and executes
socket.on('sendchat', function (data) {
insertMess(socket.username, data, socket.room);
var nd = data.split(">");
if(nd[0] == 'cmd')
{
switch(nd[1])
{
case 'admin-mess':
//Return message to user
socket.emit('updatechat', admin_name, '<font color="red">your message was sent to all rooms.</font><br>'+nd[2]);
//broadcast message to all rooms
for(var i=0; i<rooms.length; i++)
{
socket.broadcast.to(rooms[i]).emit('updatechat', '<font color="red">'+admin_name+'</font>', '<font color="red">'+nd[2]+'</font>');
}
break;
default:
socket.emit('updatechat', admin_name, 'you try to send a wrong command...');
break;
}
return;
}
// we tell the client to execute 'updatechat' with 2 parameters
io.sockets.in(socket.room).emit('updatechat', socket.username, data);
});
socket.on('switchRoom', function(newroom){
console.log('--> '+socket.username+" joined the room : "+newroom);
// leave the current room (stored in session)
socket.leave(socket.room);
//get userlist of the room
users = getRoomUsers(socket.room);
//update userlist in room
socket.broadcast.to(socket.room).emit('updateusers', users);
// join new room, received as function parameter
socket.join(newroom);
socket.emit('updatechat', admin_name, 'you are connected to "'+ newroom+'"');
// sent message to OLD room
socket.broadcast.to(socket.room).emit('updatechat', admin_name, socket.username+' has left this room');
// update socket session room title
socket.room = newroom;
socket.broadcast.to(newroom).emit('updatechat', admin_name, socket.username+' has joined this room');
socket.emit('updaterooms', rooms, newroom);
//socket.broadcast.to(newroom).emit('updateuserlist', socket.clients(newroom));
users = getRoomUsers(socket.room);
//update userlist in room
socket.broadcast.to(socket.room).emit('updateusers', users);
//update userlist for client
socket.emit('updateusers', users);
});
// when the user disconnects.. perform this
socket.on('disconnect', function(){
// echo globally that this client has left
socket.broadcast.emit('updatechat', admin_name, socket.username + ' has disconnected');
socket.leave(socket.room);
console.log('<-- '+socket.username+" has disconnected".info);
//get userlist of the room
users = getRoomUsers(rooms[0]);
//update userlist for client
socket.broadcast.emit('updateusers', users);
});
});
...
我需要将我的Titanium应用程序(iOs / Android)连接到我的服务器。 我的问题是:
某人可以有一点例子吗?
很多!
答案 0 :(得分:1)
您需要客户端库才能连接到服务器。您可以在domain.com/socket.io/socket.io.js上获取它并在本地缓存它。从那里你可以访问全局io然后连接:
var socket = io.connect();
将库复制到您的应用程序中并拥有一个每x天/周提取最新版本的后台服务可能更好。