我试图在使用socket.io (localhost:8000)的快速应用程序和使用vue-socket.io的vue应用程序(localhost:8080)之间创建一个socket.io通信。快速应用程序正在接收vue应用程序的排放,反之亦然。这是我的快递应用程序,不过我很确定问题在于vue应用程序:
后端快车
const port = 8000
const express = require("express")
const socket = require("socket.io")
const app = express()
const server = app.listen(port, () => {
console.log(`App is lisening to port ${port}...`)
} )
const io = socket(server)
io.on("connection", (socket) => {
console.log("connected to " + socket.id) //this is successfully getting logged
socket.on("chat", (msg) => {
console.log(socket.id + ": " + msg) //this is successfully getting logged when I click send
io.sockets.emit("chat", msg)
} )
} )我猜问题出在以下几个地方:
前端Vue (使用CLI)
main.js:
import Vue from 'vue'
import App from './App.vue'
import VueSocketIO from 'vue-socket.io'
import SocketIO from 'socket.io-client'
Vue.use(new VueSocketIO( {
debug: true,
connection: SocketIO('http://localhost:8000')
} )
)
Vue.config.productionTip = false
new Vue({
render: h => h(App),
}).$mount('#app')聊天组件(Chat.vue)
<template lang="html">
<div>
<input type="text" v-model="input"/>
<button @click="send">Send</button>
</div>
</template>
<script>
export default {
sockets: {
//These dont work, nothing is printed to the console
connect: function () {
console.log('socket connected')
},
chat: function (msg) {
console.log("chat message recieved!: " + msg)
}
},
data(){
return {
input: ""
}
},
methods: {
send(){
this.$socket.emit('chat', this.input) //This works and is recieved by the express app!
}
}
}
</script>有一天我一直想搞清楚这件事,却没有运气。
再一次,我的唯一目标是能够在快速应用程序前面接收排放。另外,如果有更好的方法在vue中使用socket.io而不使用vue-socket.io,我很乐意对此进行研究。
发布于 2020-06-23 18:44:18
我能够简单地使用vue-socket.io-extended而不是vue-socket.io来完成这个任务。仅此而已,没有必要对代码进行重大修改。我知道从技术上讲,这并不能解决使用vue-socket.io的问题,但在有人弄清楚这一点之前,我将把这个作为未来搜索者的答案。
https://stackoverflow.com/questions/62539525
复制相似问题