socket.io在使用广播时获取错误消息

发布于 2025-02-01 05:18:30 字数 1771 浏览 0 评论 0原文

实际上,我正在尝试使用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技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ヤ经典坏疍 2025-02-08 05:18:30

它写在socket.io文档中,如下:

请注意,广播是仅服务器的功能。

.broadcast仅在服务器端使用。您向所有连接的客户端广播了一些东西,不是客户到客户

相关文档

It is written in the socket.io documentation as follows:

Please note that broadcasting is a server-only feature.

.broadcast is only used on the server-side. You broadcast something to all connected clients, not client-to-client.

Related documentation

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文