我看了几个重叠的演示,但是我无法获得基本的发射功能。我收到与服务器文件的连接消息,但没有尝试从客户端发出。这是我在每个文件中的内容:
SocketIOManager.swift
import UIKit
class SocketIOManager: NSObject {
static let sharedInstance = SocketIOManager()
override init() {
super.init()
}
var socket: SocketIOClient = SocketIOClient(socketURL: NSURL(string: "myURL")! as URL)
func establishConnection() {
socket.connect()
}
func closeConnection() {
socket.disconnect()
}
func testSocket() {
socket.emit("test")
}
}
AppDelegate.swift
func applicationDidEnterBackground(_ application: UIApplication) {
SocketIOManager.sharedInstance.closeConnection()
}
func applicationDidBecomeActive(_ application: UIApplication) {
SocketIOManager.sharedInstance.establishConnection()
}
的ViewController
override func viewDidLoad() {
super.viewDidLoad()
SocketIOManager.sharedInstance.testSocket()
}
index.js服务器文件
io.on('connection', function(clientSocket){
console.log('a user connected');
clientSocket.on('disconnect', function(){
console.log('user disconnected');
});
clientSocket.on("test", function(){
console.log('test')
});
});
所以再次,当我得到连接和断开的打印语句时,我没有得到任何常规发射函数。
答案 0 :(得分:0)
请尝试更新index.js
由此
io.on('connection', function(clientSocket){
console.log('a user connected');
clientSocket.on('disconnect', function(){
console.log('user disconnected');
});
clientSocket.on("test", function(){
console.log('test');
});
});
因为)
的末尾缺少disconnect
。