我需要通过 socket.io 将我的iOS应用( swift )连接到 node.js 服务器。我跟着socket.io official tutorial,但我仍然卡住了。
我尝试连接到服务器并发出“authentification”,但发出后我没有收到任何反馈。
我的快速代码
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.
此“ 欢迎使用socket.io。 ”表示我已连接到服务器?
答案 0 :(得分:1)
问题是我的服务器正在运行socket.io v0.9而socket.io-client-swift支持socket.io v1.0 +。
我无法更新我的服务器,因为其他客户端正在运行C#并使用仅与socket.io v0.9兼容的socketio4net。
解决方案是使用socket.IO-objc,因为它支持socket.io v0.9。
第1步: 获取socket.IO-objc here。
第2步: 在项目中导入socket.IO-objc。 有用的帖子:https://stackoverflow.com/a/24005242/4220809
第3步: 安装依赖项(SocketRocket)。 使用cocoapods在我的swift项目中导入这个objective-c库。 教程:Using Objective-C CocoaPods libraries with Swift