Git Product home page Git Product logo

vite-socket.io-examples's Introduction

Vite Socket.IO Examples

Some demo projects for Socket.IO with Vite.

vite-socket.io-examples's People

Contributors

wdwang09 avatar

Watchers

 avatar

vite-socket.io-examples's Issues

关于 socket.io-client 的 io(url:string) 方法

您好,我在尝试编写一个与您的项目类似的 Demo,但我在配置前后端跨域时遇到了问题。

// server: running at localhost:3001
const io = new Server(server, {
  cors: {
    origin: 'http://localhost:3001'
  }
})

// client: running at localhost:3000
import { io } from 'socket.io-client'
const socket = io('/ws')

// vite.config.ts
export default defineConfig({
 // ...
  server: {
    proxy: {
      '/ws': {
        target: 'http://localhost:3001',
        ws: true,
        rewrite: (path) => path.replace(/^\/ws/, ''),
        changeOrigin: true
      }
    }
  }
})

这样在客户端无法建立 WebSocket 连接,而在 POSTMAN 中可以通过http://localhost:3001建立连接,所以我认为是前后端跨域上出了问题。
而我阅读您的代码后,发现您在客户端建立 WebSocket 连接的方式是:

import { io } from 'socket.io-client'
const socket = io('localhost:3001')

请问这里为何不需要专门在vite.config.js中配置访问服务端的跨域呢,这种设置在将后端部署到服务器后是否仍有效呢?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.