Reputation: 306
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?
Upvotes: 1
Views: 2362
Reputation: 306
The problem is that my server is running socket.io v0.9 and socket.io-client-swift suports socket.io v1.0+.
I couldn't just update my server because an other client are running C# and using socketio4net which is compatible only with socket.io v0.9.
The solution is to use socket.IO-objc because it suports socket.io v0.9.
Step 1: Get socket.IO-objc here.
Step 2: Import socket.IO-objc in your project. Useful post: https://stackoverflow.com/a/24005242/4220809
Step 3: Install dependencies (SocketRocket). Use cocoapods to import this objective-c library in my swift project. Tutorial: Using Objective-C CocoaPods libraries with Swift
Upvotes: 2