当前位置:网站首页>socket. IO cross domain stepping pit
socket. IO cross domain stepping pit
2022-06-11 06:29:00 【Xiaobai learns computer】
One 、koa combination socket.io
Back end code :
// Introduce dependencies
const koa = require("koa");
// initialization koa
const app = new koa();
// Turn on http
var server = require("http").createServer(app.callback());
// initialization socket
const io = require("socket.io")(server, {
cors: true });
// monitor
io.on("connection", (socket) => {
console.log(" Someone linked ");
// receive data
socket.on('send', function (data) {
console.log(data);
// send data
socket.emit('resend', {
msg: ` Hello ${
data.msg}`,
code: 200
});
});
});
server.listen(3000);
The front-end code :
<template>
<div class="hello">
<input type="text" v-model="msg">
<button @click="sendMsg"> send out </button>
</div>
</template>
<script>
import io from 'socket.io-client'
export default {
name: 'HelloWorld',
data () {
return {
msg: 'Welcome to Your Vue.js App'
}
},
mounted () {
const socket = io('ws://localhost:3000')
this.socket = socket
window.socket = socket
socket.on('connect', function () {
console.log(' The connection was established !')
})
socket.on('disconnect', function () {
console.log(' Disconnected ')
})
socket.on('resend', function (data) {
console.log(data)
})
},
methods: {
sendMsg () {
// Send information to the server
this.socket.emit('send', {
msg: this.msg
})
}
}
}
</script>
<style scoped>
</style>


Two 、express combination socket.io
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http, {
cors: true });
app.get('/', function(req, res){
res.send('<h1> Hello web Show </h1>');
});
io.on('connection',function(socket) {
console.log(" Someone linked ");
// receive data
socket.on('send', function (data) {
console.log(data);
// send data
socket.emit('resend', {
msg: ` Hello ${
data.msg}`,
code: 200
});
});
});
http.listen(3000, function(){
console.log('listening on *:3000');
});
The front-end code :
<template>
<div class="hello">
<input type="text" v-model="msg">
<button @click="sendMsg"> send out </button>
</div>
</template>
<script>
import io from 'socket.io-client'
export default {
name: 'HelloWorld',
data () {
return {
msg: 'Welcome to Your Vue.js App'
}
},
mounted () {
const socket = io('ws://localhost:3000')
this.socket = socket
window.socket = socket
socket.on('connect', function () {
console.log(' The connection was established !')
})
socket.on('disconnect', function () {
console.log(' Disconnected ')
})
socket.on('resend', function (data) {
console.log(data)
})
},
methods: {
sendMsg () {
// Send information to the server
this.socket.emit('send', {
msg: this.msg
})
}
}
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>


Be careful :
If cross domain error is reported , You can refer to the following solutions :
- reinstall socket.io-client
npm i socket.io-client --save
- stay vue Use in


边栏推荐
- Jenkins user rights management
- Error code in ijkplayer
- Simple understanding of pseudo elements before and after
- Convert text label of dataset to digital label
- 解决ffmpeg獲取AAC音頻文件duration不准
- Shandong University machine learning experiment 5 SVM
- Shandong University machine learning experiment 7 pca+ SVM face recognition
- Learn C language well from keywords
- About parseint()
- Handwriting promise [03] - realize multiple calls and chain calls of then method
猜你喜欢

FPGA interview notes (II) -- synchronous asynchronous D flip-flop, static and dynamic timing analysis, frequency division design, retiming
![[reading this article is enough!!! Easy to understand] confidence level understanding (95% confidence level and confidence interval)](/img/d6/e61ba5bad2b2847378c4547ce0780d.jpg)
[reading this article is enough!!! Easy to understand] confidence level understanding (95% confidence level and confidence interval)

Quantitative understanding (Quantitative deep revolutionary networks for effective information: a whitepaper)

修复鼠标右键没有vscode快捷入口的问题

CCS安装编译器的方法

PHP laravel8 send email

On cursor in MySQL

Graphsage paper reading

JS judge whether the year is a leap year and the number of days in the month

山东大学项目实训之examineListActivity
随机推荐
Simple understanding of pseudo elements before and after
022-Redis数据库0基础入门
Count the time-consuming duration of an operation (function)
EasyGBS接入的设备视频直播突然全部无法播放是为什么?数据库读写不够
Learn C language well from keywords
Verilog realizes binocular camera image data acquisition and Modelsim simulation, and finally matlab performs image display
The classification effect of converting video classification data set to picture classification data set on vgg16
Jenkins user rights management
Shuffleerror:error in shuffle in fetcher solution
Wechat applet (authorized login of TP5)
Make a small game with R language and only basic package
arguments......
fatal: refusing to merge unrelated histories
统计某次操作(函数)耗时时长
Throttling and anti shake
LeetCodeT526
Difference between foreach, for... In and for... Of
How to arrange the dataframe from small to large according to the absolute value of a column?
A multi classification model suitable for discrete value classification -- softmax regression
022 basic introduction to redis database 0