我即将编写一个非常简单的iOS应用程序。我希望应用程序使用Socket.IO连接到服务器。我已经为我的项目安装了Cocoapods的Socket.IO,一切顺利。 问题是在我运行我的服务器然后运行应用程序模拟器之后,应用程序没有连接到服务器。我没有得到任何类型的错误消息或类似的东西,但服务器应该在连接套接字时在控制台/终端上打印消息。
这是套接字管理器类
docker swarm join-token -q worker
这是AppDelegeate类:
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()
}
}
最后我的服务器代码用node.js
编写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:.
}
}
答案 0 :(得分:1)
我会尝试使用实际IP 而不是" Localhost" ,我认为它是作为一个设备连接到它自己,这就是为什么没有出错
如果你正在尝试使用模拟器或实际设备,也许你应该提一下 还检查防火墙
答案 1 :(得分:0)
尝试写 SocketIOClient(socketURL:URL(string:“http://192.168.0.8:3000”)!) 代替 SocketIOClient(socketURL:NSURL(string:“localhost:3000”)!) 并写下“你的服务器IP地址”
我希望它为你工作
答案 2 :(得分:0)
这对我有用:
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()
}
}
答案 3 :(得分:0)
// Case 1: This won't work
class Myclass{
func initSocket() {
let manager = SocketManager(socketURL: URL(string: BASE_URL)!, config: [.log(true), .compress])
socket = manager.defaultSocket
socket.on(clientEvent: .connect) {data, ack in
print("SOCKET CONNECTED")
}
}
}
// Case 2: This works
class Myclass {
let manager = SocketManager(socketURL: URL(string: BASE_URL)!, config: [.log(true), .compress])
var socket: SocketIOClient!
func initSocket() {
socket = manager.defaultSocket
socket.on(clientEvent: .connect) {data, ack in
print("SOCKET CONNECTED")
}
}
}
.connect
事件从不触发,而在服务器disconnect
事件上触发了一段时间后