0 x01 cause:
Socketio was used to set up the signaling server, express and Socketio were used as the server, and 95% of blogs on the Internet were referred to, and the problem of CORS was found. After half a day of torturing, most of the posts were not successful. Finally, I tried to make a successful attempt by referring to a post on Github.
const express = require('express');
const app = express();
const http = require('http').Server(app);
let path = require('path');
var cors = require('cors');
app.use(cors());
const io = require('socket.io')(http,{
cors: {origin:"http://127.0.0.1:8081".methods: ["GET"."POST"].credentials: true.allowEIO3: true
},
transport: ['websocket']}); io.on('connection'.function(socket) {
console.log('A user connected');
//Whenever someone disconnects this piece of code executed
socket.on('disconnect'.function () {
console.log('A user disconnected');
});
socket.on('login'.function (obj) {
console.log(obj.username);
// Send data
socket.emit('relogin', {
msg: Hi `${obj.username}`.code: 200
});
});
});
http.listen(3000.function() {
console.log('listening on *:3000');
});
Copy the code
Client code
import VueSocketIO from 'vue-socket.io'
import SocketIO from 'socket.io-client'
Vue.use(new VueSocketIO({
debug: true.connection: SocketIO('http://127.0.0.1:3000'),
extraHeaders: {
'Access-Control-Allow-Credentials':true
},
allowEIO3:true.vuex: {
store,
actionPrefix: 'SOCKET_'.mutationPrefix: 'SOCKET_'}}));Copy the code