Swift iOS应用程序不使用Socket.IO连接到服务器

我即将写一个非常简单的iOS应用程序。 我希望应用程序通过使用Socket.IO连接到服务器。 我已经为我的项目安装了Cocoapods的Socket.IO,一切进展顺利。 问题是我运行我的服务器,然后应用程序模拟器后,应用程序没有连接到服务器。 我没有得到任何types的错误消息或类似的东西,但是服务器应该在连接套接字时在控制台/terminal上打印消息。

这是套接字pipe理器类

import UIKit import SocketIO class SocketManager: NSObject { static let sharedInstance = SocketManager() override init() { super.init() } var socket: SocketIOClient = SocketIOClient(socketURL: NSURL(string: "localhost:3000")!) func establishConnection() { socket.connect() } func closeConnection() { socket.disconnect() } } 

这是AppDelegeate类:

 import UIKit @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { // Override point for customization after application launch. return true } func applicationWillResignActive(application: UIApplication) { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. } func applicationDidEnterBackground(application: UIApplication) { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. SocketManager.sharedInstance.closeConnection() } func applicationWillEnterForeground(application: UIApplication) { // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. } func applicationDidBecomeActive(application: UIApplication) { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. SocketManager.sharedInstance.establishConnection() } func applicationWillTerminate(application: UIApplication) { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. } } 

最后我的服务器代码写在node.js中

 var app = require('express')(); var http = require('http').Server(app); var io = require('socket.io')(http); app.get('/', function(req, res){ res.send('<h1>Server Chat</h1>'); }); http.listen(3000, function(){ console.log('Listening on *:3000'); }); io.on('connection', function(clientSocket){ console.log('a user connected'); }); 

我会尝试使用真正的IP而不是“本地主机 ”,我认为它是连接到自己作为一个设备,这就是为什么不是一个错误

也许你应该提到,如果你正在尝试使用模拟器或实际设备也检查防火墙

试着编写SocketIOClient(socketURL:URL(string:“ http://192.168.0.8:3000 ”)!)而不是SocketIOClient(socketURL:NSURL(string:“localhost:3000”)!)并写上“你的服务器IP地址“

我希望为你工作

这对我有用:

 dict = [.connectParams(["payloadId": getPayloadId(), "version": getVersion()])] socket = SocketIOClient(socketURL: URL(string: "http://localhost:8080")!, config: dict) socket.connect() socket.on("connect") {data, ack in if(self.socket.status == .connected) { print("socket connected") } else { self.reconnectSocket() } }