最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

当我向套接字服务器发出一些值时没有任何反应

IT培训 admin 4浏览 0评论

当我向套接字服务器发出一些值时没有任何反应

我有一个快速服务器代码,它可以像你在下面看到的那样收听套接字端口。

websocket.on('connection', (socket) => {
  console.log('A client just joined on', socket.id);
  websocket.on('messageIM', (message) => {
     console.log('msg:'+message)

});
});

我的客户端应用程序是一个react-native移动项目,其中包含如下代码:

  constructor(props) {
super(props); 
this.socket = SocketIOClient('http://192.168.140.51:3000/');

  }

我得到客户端连接'一个客户端刚加入***'登录服务器控制台,意味着与服务器的连接成功。但是当我发出一些变量......服务器端什么都不会发生!...

call()=>{
 this.socket.emit('messageIM','Hi server')
}

意思是当我在call()上面调用函数时,服务器中没有任何反应。

这是我在客户端和服务器端编写的代码,如果您有一些经验,请给予一些帮助。

回答如下:

看起来你使用了错误的变量来监听。连接事件返回您要使用的套接字。将websocket.on('messageIM'重命名为socket.on('messageIM'

websocket.on('connection', socket => {
  console.log('A client just joined on', socket.id);
  socket.on('messageIM', message => {
     console.log(`msg: ${message}`)
  });
});

当我向套接字服务器发出一些值时没有任何反应

我有一个快速服务器代码,它可以像你在下面看到的那样收听套接字端口。

websocket.on('connection', (socket) => {
  console.log('A client just joined on', socket.id);
  websocket.on('messageIM', (message) => {
     console.log('msg:'+message)

});
});

我的客户端应用程序是一个react-native移动项目,其中包含如下代码:

  constructor(props) {
super(props); 
this.socket = SocketIOClient('http://192.168.140.51:3000/');

  }

我得到客户端连接'一个客户端刚加入***'登录服务器控制台,意味着与服务器的连接成功。但是当我发出一些变量......服务器端什么都不会发生!...

call()=>{
 this.socket.emit('messageIM','Hi server')
}

意思是当我在call()上面调用函数时,服务器中没有任何反应。

这是我在客户端和服务器端编写的代码,如果您有一些经验,请给予一些帮助。

回答如下:

看起来你使用了错误的变量来监听。连接事件返回您要使用的套接字。将websocket.on('messageIM'重命名为socket.on('messageIM'

websocket.on('connection', socket => {
  console.log('A client just joined on', socket.id);
  socket.on('messageIM', message => {
     console.log(`msg: ${message}`)
  });
});
发布评论

评论列表 (0)

  1. 暂无评论