socket.io在使用广播时获取错误消息
实际上,我正在尝试使用vuejs and socket.io创建聊天应用程序,所以这是我到目前为止所做的
<script>
import {io} from "socket.io-client";
export default {
data() {
return {
socketInstance: io("http://localhost:3001"),
};
},
methods: {
async handleSendMessage(message) {
const postParams = {
ip: this.userData.ip,
message: message,
user_type: "customer",
};
const {data} = await axios.post(
"http://localhost:3001/api/message",
postParams
);
this.messages.push(data.message);
this.socketInstance.broadcast.emit("new_message", {
message: data.message,
socket_id: this.socketInstance.id,
});
//HERE AM GETTING ERROR WHILE USING BROADCAST
}
},
mounted() {
this.socketInstance.on("new_message", ({message, socket_id}) => {
if (message.ip == this.userData.ip) {
this.messages.push(message);
}
});
},
};
</script>
基本上 handlesendMessage 函数负责发送消息并发出事件,但是当我
this.socketInstance.emit("new_message", {
message: data.message,
socket_id: this.socketInstance.id,
});
这样做时,它可以正常工作,但是它可以正常运行如您所知,我会收到重复的消息,所以我使用
this.socketInstance.broadcast.emit("new_message", {
message: data.message,
socket_id: this.socketInstance.id,
});
并批评了错误,
Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'emit')
我不知道为什么会遇到此错误。 对不起,我的英语不好。
actually am trying to create chat app using vueJS and socket.io so here is what i have done so far
<script>
import {io} from "socket.io-client";
export default {
data() {
return {
socketInstance: io("http://localhost:3001"),
};
},
methods: {
async handleSendMessage(message) {
const postParams = {
ip: this.userData.ip,
message: message,
user_type: "customer",
};
const {data} = await axios.post(
"http://localhost:3001/api/message",
postParams
);
this.messages.push(data.message);
this.socketInstance.broadcast.emit("new_message", {
message: data.message,
socket_id: this.socketInstance.id,
});
//HERE AM GETTING ERROR WHILE USING BROADCAST
}
},
mounted() {
this.socketInstance.on("new_message", ({message, socket_id}) => {
if (message.ip == this.userData.ip) {
this.messages.push(message);
}
});
},
};
</script>
Basically handleSendMessage function is responsible for sending message and emit an event but when I do
this.socketInstance.emit("new_message", {
message: data.message,
socket_id: this.socketInstance.id,
});
then it's works fine but as you know i will get duplicate messages so i used
this.socketInstance.broadcast.emit("new_message", {
message: data.message,
socket_id: this.socketInstance.id,
});
and got following error
Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'emit')
I don't know why am getting this error.
Sorry for my bad English.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它写在socket.io文档中,如下:
.broadcast
仅在服务器端使用。您向所有连接的客户端广播了一些东西,不是客户到客户。相关文档
It is written in the socket.io documentation as follows:
.broadcast
is only used on the server-side. You broadcast something to all connected clients, not client-to-client.Related documentation