简体   繁体   English

如何在 nuxt-socket.io 上从客户端到服务器发出事件

[英]how to emit event on nuxt-socket.io from client to server

im learning to use Nuxt in the past i have worked with socket.io and vue but this time i am using nuxt-socket-io https://nuxt-socket-io.netlify.app/我过去在学习使用 Nuxt,我曾使用过 socket.io 和 vue,但这次我使用的是 nuxt-socket-io https://nuxt-socket-io.netlify.app/

My problem is that I cannot send events from client to server我的问题是我无法将事件从客户端发送到服务器

here an example:这里有一个例子:

in socket-io-server.js在 socket-io-server.js 中

io.emit ("eventTry")

at homepage.vue在主页.vue

mounted () {

     this.socket = this. $ nuxtSocket ({
       name: "main",
     });

     this.socket.on ("eventTry", () => {
         console.log ("this Work Good");
     })

   },

and this works fine这很好用

but when I try to send from the client to the server nothing works example:但是当我尝试从客户端发送到服务器时,没有任何工作示例:

at homepage.vue在主页.vue

this.socket.emit ("eventTryClient");

and in socket-io-serve.js:在 socket-io-serve.js 中:

io.on ("eventTryClient", () => {
         console.log ("something should happen here")
     })

and this DONT WORK这不工作

I've really looked everywhere and I can't find a solution to this problem that should be so simple, could someone help me?我真的到处找了,我找不到解决这个问题的方法,应该这么简单,有人可以帮助我吗? I need to have a bidirectional communication, which is the best of socket.io I think我需要双向通信,这是我认为最好的socket.io

Thanks in advance for your help <3预先感谢您的帮助 <3

You need to listen for events on the socket .您需要监听套接字上的事件。 Listening on a namespace, eg your io variable, won't actually listen to events sent by the client.侦听命名空间,例如您的io变量,实际上不会侦听客户端发送的事件。 More info here , although it doesn't explicitly state to listen on the client socket.更多信息here虽然它没有明确声明要侦听客户端套接字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Socket.io,无法从客户端发出自定义事件 - Socket.io, can't emit custom event from client 如何从客户端(Javascript)使用Socket IO来(-)。emit(-)消息 - How to consume the Socket IO to(-).emit(-) messages from client (Javascript) 如何从路由文件中发出socket.io中的事件? - How to emit an event in socket.io from the routes file? socket.io,服务器在客户端为该事件设置侦听器之前发出事件 - socket.io, server emit event before client set up listener for that event Java脚本和套接字IO:无法从客户端向服务器发出值来操纵服务器上的变量 - Java Script & Socket IO: Cannot emit a value from client to server to manipulate a variable on the server 如何从nuxt插件发出事件? - How to emit an event from a nuxt plugin? socket.io在连接时不会从服务器向客户端发出事件 - socket.io not emitting event from server to client on connection 使用socket.io和node,是否有可能响应未由浏览器触发的事件而从服务器发射到浏览器? - With socket.io and node, is it possible to emit to browser from server in response to an event not triggered by the browser? socket.io - 无法从服务器向特定客户端发送数据 - socket.io - can't emit data from server to specific client Socket.io-如果“客户端”发出错误数据,防止服务器崩溃 - Socket.io - Prevent server crash if “client” emit wrong data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM