[英]Connecting Socket.io using VUE or vue-socket.io
I'm trying to connect using socket.io (client)
, websocket.org (server)
in vue.js
.我正在尝试在 vue.js 中使用
socket.io (client)
、 websocket.org (server)
进行vue.js
。 After I reading all examples I'm able to connect to the socket but once I emit the event BOARD_ID
I'm not receiving anything back.在阅读完所有示例后,我可以连接到套接字,但是一旦发出事件
BOARD_ID
,我就没有收到任何回复。 As a socket server, I'm using wss://echo.websocket.org
and I tried a standalone example and it works.作为套接字服务器,我使用的是
wss://echo.websocket.org
并且我尝试了一个独立的示例并且它可以工作。
main.js main.js
import Vue from "vue";
import VueSocketIO from "vue-socket.io";
import router from "./router";
import App from "./App.vue";
Vue.config.productionTip = false;
Vue.use(
new VueSocketIO({
debug: true,
connection: "wss://echo.websocket.org"
})
);
new Vue({
sockets: {
connect: function () {
console.log("Socket Connected");
}
},
router,
render: (h) => h(App)
}).$mount("#app");
Board.vue Board.vue
<template>
<div id="Board">
<h3>Board ID is :{{ this.$route.params.Bid }}</h3>
<button v-on:click="goToHome">Go To Home</button>
<button v-on:click="updateBoard">Send To Socket</button>
<p>{{ message }}</p>
</div>
</template>
<script>
export default {
name: "Board",
data() {
return {
domain: "https://someapp.app",
path: "https://someapp.app/board/" + this.$route.params.Bid,
message: "Not Recived",
};
},
methods: {
goToHome() {
this.$router.push({ name: "Home" });
},
updateBoard: function (e) {
this.$socket.emit("BOARD_ID", this.$route.params.Bid);
console.log("Sent...");
},
listenBoard: function () {
this.$socket.on("BOARD_ID", (data) => {
console.log(data);
this.message = "Recived: " + data;
});
this.sockets.subscribe("BOARD_ID", (data) => {
console.log(data);
this.message = "Recived: " + data;
});
console.log("Litening...");
}
},
mounted: function () {
this.listenBoard();
},
};
</script>
A Socket.IO client requires a Socket.IO server to communicate. Socket.IO 客户端需要Socket.IO 服务器进行通信。 The
echo.websocket.org
is a plain WebSocket server and cannot be connected to using a Socket.IO client. echo.websocket.org
是普通的 WebSocket 服务器,无法使用 Socket.IO 客户端连接。
If you want to connect to echo.websocket.org
you are looking for the WebSocket API .如果你想连接到
echo.websocket.org
你正在寻找WebSocket API 。 Perhaps you are only trying to use the echo
server for testing, and would like to use Socket.IO.也许您只是想使用
echo
server 进行测试,并且想使用 Socket.IO。 In that case I recommend setting up your own Socket.IO server.在这种情况下,我建议设置您自己的 Socket.IO 服务器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.