簡體   English   中英

當我向套接字服務器發送一些值時,什么也沒有發生

[英]nothing happens when i emit some value to socket server

我有一個expressjs服務器代碼,它偵聽套接字端口,如下所示。

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

});
});

我的客戶端應用程序是一個本機移動項目,其中包含如下代碼:

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

  }

並且我獲得客戶端連接“在***上剛加入一個客戶端”在服務器控制台上登錄,這意味着與服務器的連接已成功。 但是當我發出一些變量....在服務器端什么也不會發生!...

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

意味着當我在call()函數上方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}`)
  });
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM