无法与用户连接"来自Socket.IO的反馈

时间:2017-01-16 00:18:16

标签: ios node.js swift sockets socket.io

我尝试使用Socket.io将我在Xcode中的iOS应用程序连接到我的Node服务器,但我没有从Socket.io获得任何反馈。这就是我的 ViewController.swift 包含:

import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    SocketIOManager.sharedInstance.establishConnection()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


}

这是我的 SocketIOManager.swift 文件包含的内容:

import UIKit

  class SocketIOManager: NSObject {
  static let sharedInstance = SocketIOManager()

var socket: SocketIOClient = SocketIOClient(socketURL: NSURL(string: "http://localhost:3000")! as URL)

override init() {
    super.init()
}

func establishConnection() {
    socket.connect()
}

func closeConnection() {
    socket.disconnect()
}

}

我的AppDelegate:

import UIKit
import CoreData

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    return true
}

func applicationWillResignActive(_ application: UIApplication) {

}

func applicationDidEnterBackground(_ application: UIApplication) {
    SocketIOManager.sharedInstance.closeConnection()

}

func applicationWillEnterForeground(_ application: UIApplication) {

}

func applicationDidBecomeActive(_ application: UIApplication) {
    SocketIOManager.sharedInstance.establishConnection()

}

最后我的服务器端

var app = require ('http').createServer();

app.listen(3000) 

  //Run Server
  var io = require('socket.io')(app);{
  console.log('Listening on *:3000');
  };

io.on('connection', function(socket){
 console.log('a user connected');
 socket.on('disconnect', function(){
 console.log('user disconnected');
   });
 });

当我启动服务器时,我得到Listening on *:3000。然而,当在XCode中启动iphone模拟器时,我认为在终端中得到a user connected但我得不到任何反馈。否 a user connected 没有 Errors 没有任何内容。我提供了所有文件,因为我迷失了问题所处的位置和地点。有人可以帮助我。

0 个答案:

没有答案