socket.io 服务器 Node.js 和 esbuild

发布于 2025-01-10 05:25:26 字数 1984 浏览 0 评论 0原文

我正在尝试使用 socket.io 设置一个服务器(API),并在它们之间连接一个客户端,这是我的 esbuild 配置,使用 esbuild-serve 进行热重载(客户端在端口 7000 上运行,并且BE on 8000)

import esbuildServe from 'esbuild-serve'

esbuildServe(
  {
    logLevel: 'info',
    entryPoints: ['src/index.js'],
    // platform: 'node',
    // target: ['node16.13'],
    // format: 'cjs',
    bundle: true,
    outfile: 'dist/main.js',
    external: ['socket.io'],
    sourcemap: true,
    minify: true
  },
  {
    port: 8000,
    root: 'dist'
  }
)

这是我的项目树,Docker 只需使用 Node 16 基础镜像并运行命令 node esbuild.config.js 来启动 dev 热重载服务器并实例化2 个 node.js 图像公开 7000(客户端)、8000(BE)、8001(websocket 端口)

在此处输入图像描述

客户端代码似乎工作正常:

import { io } from 'socket.io-client'

const socket = io('localhost:8001')

socket.on('connect', (socket) => {
  console.log(socket)
})

socket.on('message', function (data) {
  console.log('message', data.message)
})

这是 BE 代码似乎没有正确捆绑

// import { createServer } from 'http'
import { Server } from 'socket.io'

// const httpServer = createServer()

// const io = new Server(httpServer, {
const io = new Server({
  port: 8001,
  cors: {
    origin: '*'
  }
})


io.on('connection', function (socket) {
  socket.emit('message', { message: 'A new user has joined!' })
})

// httpServer.listen(8001)

// io.emit('connection', (socket) => {
//   return socket
// })

通过删除 external: ['socket.io'], 它开始抱怨 node_module 缺少依赖项,因此我按照建议启用了这些设置

platform: 'node',
target: ['node16.13'],
format: 'cjs',

:加载 localhost:8000 后的版本我遇到此错误 Uncaught ReferenceError: require is not Defined

更新:

我已将格式 cjs 更改为 esm现在似乎编译但没有抛出这个新错误:

Uncaught TypeError: 无法解析模块说明符“socket.io”。相对引用必须以“/”、“./”或“../”开头

I'm trying to setup a server (API) with socket.io and a client connected between them, this is my esbuild config using hot-reload with esbuild-serve (client it's running on port 7000 and BE on 8000)

import esbuildServe from 'esbuild-serve'

esbuildServe(
  {
    logLevel: 'info',
    entryPoints: ['src/index.js'],
    // platform: 'node',
    // target: ['node16.13'],
    // format: 'cjs',
    bundle: true,
    outfile: 'dist/main.js',
    external: ['socket.io'],
    sourcemap: true,
    minify: true
  },
  {
    port: 8000,
    root: 'dist'
  }
)

and this is my project tree, Docker simply use a node 16 base image and run the command node esbuild.config.js to start dev hot-reload server and instantiate the 2 node.js images exposing 7000(client), 8000(BE), 8001(websocket port)

enter image description here

Client code seems to work OK:

import { io } from 'socket.io-client'

const socket = io('localhost:8001')

socket.on('connect', (socket) => {
  console.log(socket)
})

socket.on('message', function (data) {
  console.log('message', data.message)
})

and this is the BE code that doesn't seem to be bundled correctly

// import { createServer } from 'http'
import { Server } from 'socket.io'

// const httpServer = createServer()

// const io = new Server(httpServer, {
const io = new Server({
  port: 8001,
  cors: {
    origin: '*'
  }
})


io.on('connection', function (socket) {
  socket.emit('message', { message: 'A new user has joined!' })
})

// httpServer.listen(8001)

// io.emit('connection', (socket) => {
//   return socket
// })

By removing the external: ['socket.io'], it start complaining about missing dependencies from the node_module so I enable these settings as it suggest:

platform: 'node',
target: ['node16.13'],
format: 'cjs',

But then the compiled version once I load localhost:8000 I have this error Uncaught ReferenceError: require is not defined

UPDATE:

I've changed the format cjs to esm and now seems to compile but not it's throwing this new error:

Uncaught TypeError: Failed to resolve module specifier "socket.io". Relative references must start with either "/", "./", or "../"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文