简体   繁体   English

Vue.js + Nuxt.js:无法使用 socket.io-client 连接套接字服务器

[英]Vue.js + Nuxt.js: Can't connect with socket server using socket.io-client

I am using for client and server sides: socket.io-client v3.0.5我在客户端和服务器端使用:socket.io-client v3.0.5

index.vue索引.vue

import socket from '~/plugins/socket.js'

<script>
    mounted() {
        socket.open()
    }
</script>

plugins/socket.js插件/socket.js

import io from 'socket.io-client'

const options = {
  path: '/socket.io',
  transports: ['websocket'],
  forceNew: true,
  reconnectionAttempts: 3,
  timeout: 2000,
  reconnection: false,
}
const socket = io(process.env.PROXY_SOCKET_URL, options)

export default socket

nuxt.config.js nuxt.config.js

plugins: [
    '~/plugins/socket.js',
],

And on mount i got this: 2 commas after '40' code in data of ws在山上我得到了这个: ws 数据中“40”代码后的 2 个逗号

My client side and server side not connecting because of 2 commas in data.由于数据中有 2 个逗号,我的客户端和服务器端未连接。 I tested my socket server via SocketIo Client tool and it works.我通过 SocketIo 客户端工具测试了我的套接字服务器,它工作正常。 My frontend and backend in docker.我的前端和后端在 docker。

How can i remove those commas?我怎样才能删除那些逗号?

You can use "nuxt-socket.io"(v1.1.18) module instead of socket.io-client.您可以使用“nuxt-socket.io”(v1.1.18) 模块代替 socket.io-client。 It helped me connect front and server.它帮助我连接前端和服务器。

Here my realization:这是我的实现:

nuxt.config.js nuxt.config.js

modules: [
    'nuxt-socket-io',
],
io: {
    // module options
    sockets: [
      {
        name: 'main',
        default: true,
        url: process.env.PROXY_SOCKET_URL,
      },
    ],
  },

index.vue索引.vue

<script>
    mounted() {
      window.socket = this.$nuxtSocket({
        path: '/socket.io',
        transport: ['websocket'],
      })
      window.socket.open()
    }
</script>

I've been playing with this the past few days, and have a module-free solution.过去几天我一直在玩这个,并且有一个无模块的解决方案。

In order to run the socket.io server on the same server that nuxt is providing we need to get access to Nuxt's server.为了在 nuxt 提供的同一台服务器上运行 socket.io 服务器,我们需要访问 Nuxt 的服务器。 Luckily, Nuxt's listen hook provides the server once it has been started.幸运的是,Nuxt 的监听钩子在服务器启动后提供了它。 We can use this server to start our socket.io server by adding the following to nuxt.config.ts :我们可以使用此服务器通过将以下内容添加到nuxt.config.ts来启动我们的 socket.io 服务器:

import startSocketServer from "./server/sockets"

defineNuxtConfig({
  ...
  hooks: { listen: (server) => startSocketServer(server) },
  ...
})

and inside of ~/server/sockets/index.ts we export a function that accepts this server and spins up the socket.io server:~/server/sockets/index.ts中,我们导出一个 function 接受这个服务器并启动 socket.io 服务器:

import { Server as NuxtServer } from 'node:http'
import {  Socket, Server } from "socket.io";

export default (nuxtServer: NuxtServer) => {
  const io = new Server(nuxtServer)
  io.on("connection", (socket: Socket) => {
    socket.emit('message', "Hello World")
  });
};

That's it for the server!这就是服务器!

If we now have an app.vue that looks like this:如果我们现在有一个如下所示的 app.vue:

<script setup lang="ts">
import { io } from "socket.io-client";
const socket = io();
onMounted(() => socket.connect());
onUnmounted(() => socket.close());

const message = ref("");
socket.on("message", (data: string) => (message.value = data));
</script>

<template>
  <div>{{ message }}</div>
</template>

We should see "Hello world" from the server when we load the page.当我们加载页面时,我们应该从服务器看到“Hello world”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM