【问题标题】:Socket.io browser client not receiving emit events from socket.io clientSocket.io 浏览器客户端未接收到来自 socket.io 客户端的发射事件
【发布时间】:2016-06-16 06:51:20
【问题描述】:

带有 SOCKET.IO SWIFT 的 IOS 客户端:

class SignupViewController: UIViewController{

let socket = SocketIOClient(socketURL: NSURL(string: "http://localhost:3000")!)

override func viewDidLoad() {
    super.viewDidLoad()
}

@IBAction func createUser(sender: AnyObject) {
    LoginUser(email: emailInput.text!, password: passwordInput.text!){ results in
        self.addHandlers()
        self.socket.connect()
    }
}

private func addHandlers(){
    socket.on("connect") {data, ack in
        self.socket.emit("new-device-connect", "ascascasc")
    }
}

服务器:

// start the server if `$ node server.js`
if (require.main === module)
app.io = require('socket.io')(app.start());
//app.start();
app.io.on('connection', function(socket){
  console.log("client connected");  

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

浏览器客户端

var socket = io('http://localhost:3000');

socket.on('connect', function(){
    socket.on("new-device-connect", function(d){
        console.log("a new client is connected");   
    });
    socket.on("disconnect", function(){

    });
});

当我在我的 IOS 客户端名称 new-device-connect 中发出事件时,我需要在浏览器客户端上调用该事件以显示连接的新 IOS 设备,但浏览器客户端未接收到 new-device-connect 事件。

【问题讨论】:

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


    【解决方案1】:

    我会在您的服务器上放置一个 socket.on("new-device-connect"),我还没有进行移动开发,但我认为您正在将该信息传递给您的服务器而不是浏览器客户端。然后 Socket.io 应该正确地向浏览器发出您的调用。

    【讨论】:

    • 谢谢兄弟。今晚我可以好好休息,现在可以抽根烟了。我知道我错过了一些东西。我想如果我从客户端发出一个事件,服务器和客户端都会监听该事件,因为它们在同一个套接字和端口上。
    猜你喜欢
    • 2019-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多