当前位置:网站首页>Wechat applet simply realizes chat room function
Wechat applet simply realizes chat room function
2022-06-25 06:17:00 【debug013】
<?php
$server = new swoole_websocket_server("0.0.0.0", 9501);
$server->on('open', function (swoole_websocket_server $server, $request) {
file_put_contents( __DIR__ .'/log.txt' , $request->fd);
});
$server->on('message', function (swoole_websocket_server $server, $frame) {
global $client;
$data = $frame->data;
$fd=$frame->fd;
$m = file_get_contents( __DIR__ .'/log.txt');
for ($i=1 ; $i<= $m ; $i++) {
echo PHP_EOL . ' i is ' . $i . ' data is '.$data . ' m = ' . $m;
if($i->$fd){
$arr=json_decode($data,true);
$arr['
name
']=" I ";
$arr['class']=1;
$data=json_endode($arr);
}
$server->push($i, $data );
}
});
$server->on('close', function ($ser, $fd) {
echo "client {$fd} closed\n";
});
$server->start();
Next XML page
<view>
<view class="sendmessage">
<input id="message" bindinput="inputMessage" placeholder=" Please input chat content "></input>
<button bindtap="sendMessage"> send out </button>
</view>
</view>
<view>
<scroll-view scroll-y="true" class="history">
<view wx:for="{
{message}}">
<block wx:if="{
{item.class == 1}}">
<view class="user">{
{item.name}} say :{
{item.msg}}</view>
</block>
<block wx:else>
<view class="service">{
{item.name}} say :{
{item.msg}}</view>
</block>
</view>
</scroll-view>
</view>
// Get application instance
var app = getApp()
var socketOpen="false"
var inputMessage=""
var i=0
var msgInfo={}
Page({
data: {
message:""
},
// Get input box information
inputMessage: function(e) {
inputMessage=e.detail.value
},
// Click event
sendMessage:function(){
if (socketOpen) {
var nickname = wx.getStorageSync('nickname')
if (nickname=="") {
nickname=" tourists "
}
var data1='{"name":"'+nickname+'","msg":"'+inputMessage+'"}'
wx.sendSocketMessage({
data:data1
})
}
},
// Page load event
onLoad: function () {
var that=this
wx.connectSocket({
url: 'ws://192.168.1.181:9502/swoole/ws_server.php'
})
wx.onSocketOpen(function(res) {
socketOpen="true"
})
wx.onSocketMessage(function(res) {
i=i+1
//console.log(res)
var re=JSON.parse(res.data)
msgInfo[i]=re
console.log(msgInfo)
if(res.data){
that.setData({
message:msgInfo
})
}
})
}
})
边栏推荐
- Laravel8 fill data
- JS implementation mouse can achieve the effect of left and right scrolling
- PHP output (print) log to TXT text
- Observation configuring wmic
- How SAP ui5 device type detection device API works
- Various errors and solutions encountered when deploying SAP ui5 application to ABAP server with SAP Fiori tools
- Go language map sorting (key/value sorting)
- [Suanli network] technological innovation of Suanli Network -- Key Technologies of green and security
- Global and Chinese gallium nitride (GAN) market output value scale forecast and application prospect analysis report 2022
- How the sap ui5 framework performs single step debugging of batch requests
猜你喜欢

Click to send text messages without response is a common problem for many users in building the elegant grass Dragonfly Q system - solve the problem of clicking to send text messages without response

Laravel8 fill data

Wireless industrial Internet of things data monitoring terminal
Do you know what a three-tier architecture is?

Socket, network model notes
Wind farm visualization: wind farm data

MySQL tuning -- 02 -- slow query log
Go quiz: considerations for function naming return value from the go interview question (more than 80% of people answered wrong)

General test point ideas are summarized and shared, which can be directly used in interview and actual software testing
The e-book "action guide for large organizations to further promote zero code application platform" was officially released!
随机推荐
SAP ui5 beginner tutorial 25 - using proxy server to solve the cross domain problem of SAP ui5 application accessing remote OData service trial version
Socket, network model notes
Noi Mathematics: Dirichlet convolution
[interview with a large factory] meituan had two meetings. Was there a surprise in the end?
Copying DNA
Vscode voice notes to enrich information (Part 1)
Differences and connections between sap ui5 and openui5
Monitoring access: how to grant minimum WMI access to the monitoring service account
SAP ui5 application development tutorial 32 - how to create a custom SAP ui5 control
Tablespace free space
Part 33 of SAP ui5 application development tutorial - trial version of responsiveness of SAP ui5 applications
MySQL transaction learning notes (I) first encounter
SAP ui5 beginner tutorial No. 27 - unit test tool quNit introduction trial version for SAP ui5 application
Do you know what a three-tier architecture is?
Tail command – view the contents at the end of the file
[kicad image] download and installation
The elephant turns around and starts the whole body. Ali pushes Maoxiang not only to Jingdong
Pre knowledge of asynchronous operation
Global and China financial guarantee marketing strategy and channel dynamic construction report 2022
Count the grid