反应本机套接字io没有从客户端发出事件 [英] React native socket io no events being emitted from client

查看:37
本文介绍了反应本机套接字io没有从客户端发出事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试将 socket.io-client 与 react-native(目前为 ios)一起使用,到目前为止,来自客户端的连接/接收服务器端事件似乎工作正常.但是我似乎无法从客户端发出任何事件?

Trying to use socket.io-client with react-native (ios for now), so far connection / receiving server side events from client seems to be working fine. However I can't seem to emit any events from the client?

客户

var socket = io("http://localhost:3000");
    socket.on('connect', function(){
        socket.on('ping', function(e) {
            console.log('Server emitted ping: ' + e);
            socket.emit('pong', 'hi server!');
        });
        socket.on('disconnect', function(){
            console.log("disconnect");
        });
    });

服务器(Node.js)

Server(Node.js)

var io = require('socket.io')(server);
io.on('connection', function (socket) {
  console.log('connected...');
  socket.on('pong', function (data) {
    console.log("Hmm?");
    console.log(data);
  });

  setTimeout(function() {
    console.log("Saying hello");
    socket.emit('ping', { message: 'Hello from server ' + Date.now() });

  }, 1000);
});

所以从服务器端,我看到了日志

So from the server side, I see the logs

connected...
Saying hello

在客户端我看到服务器发出 ping...",但 pong 事件似乎没有做任何事情?我尝试通过 StackOverflow 中提到的解决方案捕获服务器上的所有事件,但看起来没有来自客户端的事件.有任何想法吗?

And in the client I see "Server emitted ping...", but the pong event doesn't seem to be doing anything? I tried catching all events on the server through solutions mentioned in StackOverflow, but it looked like no event was coming from the client. Any ideas?

使用最新的 RN 版本 0.31.

Using latest RN version 0.31.

当我第一次在 Xcode 中运行应用程序时也看到这个错误,这可能是原因吗?:

Also seeing this error when I first run the app in Xcode, could it be the reason?:

[warn][tid:main][RCTEventEmitter.m:52] Sending `websocketFailed` with no listeners registered.

推荐答案

请尝试:

io.sockets.on('connection', function(socket) {
    ....
})

这篇关于反应本机套接字io没有从客户端发出事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆