wdwang09 / vite-socket.io-examples Goto Github PK
View Code? Open in Web Editor NEWSeveral examples for Socket.IO with Vite, Vue and React.
Several examples for Socket.IO with Vite, Vue and React.
您好,我在尝试编写一个与您的项目类似的 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
中配置访问服务端的跨域呢,这种设置在将后端部署到服务器后是否仍有效呢?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.