我需要通過我的iOS應用(迅速)連接到的node.js服務器socket.io連接我的快捷應用的node.js。我跟着socket.io official tutorial,但我仍然卡住了。無法通過socket.io
我試圖連接到服務器,只是發出「認證」,但我沒有收到任何反饋後發出。
我的SWIFT代碼
class Socket{
var socket = SocketIOClient(socketURL: "localhost:8000", options:[.Log(true), .ForcePolling(true)])
init(){
self.addHandles()
self.socket.connect()
socket.connect()
self.socket.emit("authentification","gaamy#test")
}
func addHandles(){
self.socket.on("connect") {data, ack in
print("socket connected")
print(data)
}
self.socket.on("error") {data in
print("socket ERROR")
print(data)
}
self.socket.on("reponse connection") {data in
print("reponse connection")
print(data)
}
self.socket.onAny {
print("Got event: \($0.event), with items: \($0.items!)")
}
}
}
Node.js的socket.on:
io.sockets.on('connection', function (socket) {
socket.on('authentification', function(info) {
var res = info.split("#");
var username = res[0];
var password = res[1];
userModel.findOne({'username':username}, function(err, user){
if(err){
io.sockets.socket(socket.id).emit('reponse connection', 'false#Impossible de se connecter a la base de données');
io.sockets.socket(socket.id).disconnect();
}
else if(!user){
io.sockets.socket(socket.id).emit('reponse connection', "false#Nom d'utilisateur invalide");
io.sockets.socket(socket.id).disconnect();
}
else if (user.password == password){
var stringUsers = username;
for(var clef of userMap.keys()){
stringUsers = stringUsers + '+' + clef;
}
// pour eviter le doublon de username
if(typeof(userMap.get(username))==="undefined")
{
userMap.set(username, socket.id);
io.sockets.socket(socket.id).emit('reponse connection', 'true#' + stringUsers);
socket.broadcast.emit("userConnected",username);
socket.broadcast.emit("message", username+" Is Now Connected!");
socket.username = username;
socket.host = 0;
console.log(username,' Is Now Connected!');
}
else{
io.sockets.socket(socket.id).emit('reponse connection', "false#Nom d'utilisateur invalide");
io.sockets.socket(socket.id).disconnect();
}
}
else{
io.sockets.socket(socket.id).emit('reponse connection', 'false#Mot de pass invalide');
io.sockets.socket(socket.id).disconnect();
}
});
});
有Xcode的輸出:
2015-12-03 13:03:21.267 Projet3[26084:369975] Log SocketIOClient: Adding handler for event: reponse connection
2015-12-03 13:03:21.268 Projet3[26084:369975] Log SocketIOClient: Adding handler for event: error
2015-12-03 13:03:21.268 Projet3[26084:369975] Log SocketIOClient: Adding handler for event: connect
2015-12-03 13:03:21.269 Projet3[26084:369975] Log SocketIOClient: Adding engine
2015-12-03 13:03:21.271 Projet3[26084:369975] Log SocketEngine: Starting engine
2015-12-03 13:03:21.271 Projet3[26084:369975] Log SocketEngine: Handshaking
2015-12-03 13:03:21.272 Projet3[26084:369975] Log SocketEngine: Doing polling request
2015-12-03 13:03:21.399 Projet3[26084:370051] Log SocketEngine: Got polling response
2015-12-03 13:03:21.400 Projet3[26084:370051] Log SocketEngine: Got message: Welcome to socket.io.
這種「歡迎插座。 io。「表示我是conn服務器?
我剛剛意識到服務器端的socket.io版本是0.9。這可能是一個問題嗎? –