当前位置:网站首页>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


边栏推荐
- C语言大战“扫雷”
- Login and registration based on servlet, JSP and MySQL
- 021-MongoDB数据库从入门到放弃
- Handwritten promise [05] - exception capture of promise method and optional parameters of then method implementation
- Why is it that the live video of the devices connected to easygbs suddenly cannot be played? Insufficient database read / write
- Docker installation of MySQL and redis
- Convert text label of dataset to digital label
- Vulnhub's breach1.0 range exercise
- FIFO最小深度计算的题目合集
- 解决ffmpeg獲取AAC音頻文件duration不准
猜你喜欢

PHP laravel8 send email

修复鼠标右键没有vscode快捷入口的问题
![Resolve typeerror: ctx injections. tableRoot.$ scopedSlots[ctx.props.column.slot] is not a function](/img/29/de95cef86bd75107555b212f01ebb9.jpg)
Resolve typeerror: ctx injections. tableRoot.$ scopedSlots[ctx.props.column.slot] is not a function

autojs,读取一行删除一行,停止自己外的脚本

关于SIoU的原理和代码实现(回顾IoU、GIoU、DIoU、CIoU)

解决ffmpeg获取AAC音频文件duration不准

235-二叉搜索树的最近公共祖先

Review Servlet

What is sentinel produced by Ali?

Learn C language well from keywords
随机推荐
Teach everyone how to implement an electronic signature
About parseint()
instanceof到底是怎样判断引用数据类型的!
Sqoop installation tutorial
617. 合并二叉树
This point of arrow function
Docker安装Mysql、Redis
Who is stronger, zip or 7-Zip, and how to choose?
End of 2021 graphics of Shandong University
Shandong University machine learning experiment 7 pca+ SVM face recognition
通过两种方式手写一个消息队列
Communication between different VLANs
[]==![]
Detailed installation instructions for MySQL
FPGA面试题目笔记(三)——跨时钟域中握手信号同步的实现、任意分频、进制转换、RAM存储器等、原码反码和补码
Ethical discussion on reptile Technology
Résoudre le problème de la durée inexacte du fichier audio AAC obtenu par ffmpeg
go的fmt包使用和字符串的格式化
Handwritten promise [05] - exception capture of promise method and optional parameters of then method implementation
CCS安装编译器的方法