I need to connect my iOS app (swift) to the node.js server via socket.io. I followed the socket.io official tutorial, but I'm still stuck.
I'm try to connect to the server and simply emit "authentification", but I don't receive any feedback after the emit.
My swift code
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!)")
}
}
}
The 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();
}
});
});
There is the Xcode output:
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.
This "Welcome to socket.io." means I am connected to the server?