[英]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.