请问微信小程序使用socketio通讯的代码前后端怎么写?
网友回复
1、先下载weapp.socket.io.js, 下载地址https://down.bfw.wiki/file/16133523860652130077.html
2、将weapp.socket.io.js放到微信小程序的目录下
const io = require('../../weapp.socket.io.js') const socket = io('ws://ip或域名:端口') Page({ data: { logs: [] }, onLoad: function () { socket.on('connect', () => { console.log('connection created.') }); socket.on('new message', d => { const { username, message } = d; console.log('received: ', username, message) }); socket.emit('add user', "Jack"); socket.emit('new message', "Jack"); } })
好了,我们再试试后端代码怎么写?后端我们是workman来实现,具体代码如下:
<?php use Workerman\Worker; use Workerman\WebServer; use Workerman\Autoloader; use PHPSocketIO\SocketIO; // composer autoload require_once '/data/server/phpsocket/vendor/autoload.php'; $io = new SocketIO(9097); $io->on('connection', function($socket){ $socket->addedUser = false; $socket->emit('new message', array( 'username'=> "tet", 'message'=> "dddd" )); // when the client emits 'new message', this listens and executes $sock...
点击查看剩余70%
python如何实现torrent的服务端进行文件分发p2p下载?
如何在浏览器中录制摄像头和麦克风数据为mp4视频保存下载本地?
go如何编写一个类似docker的linux的虚拟容器?
python如何写一个bittorrent的种子下载客户端?
ai能通过看一个网页的交互过程视频自主模仿复制网页编写代码吗?
ai先写功能代码通过chrome mcp来进行测试功能最后ai美化页面这个流程能行吗?
vue在手机端上下拖拽元素的时候如何禁止父元素及body的滚动导致无法拖拽完成?
使用tailwindcss如何去掉响应式自适应?
有没有直接在浏览器中运行的离线linux系统?
nginx如何保留post或get数据进行url重定向?