当前位置:网站首页>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
})
}
})
}
})
边栏推荐
- Research Report on global and Chinese vaccine market profit forecast and the 14th five year plan development strategy 2022-2028
- Day21 JMeter usage basis
- Part 34 of SAP ui5 application development tutorial - device adaptation of SAP ui5 application based on device type
- Processes and threads - concepts and process scheduling
- Ifconfig command – displays or sets network devices
- Websocket in the promotion of vegetable farmers
- Asemi fast recovery diode us1m parameters, us1m recovery time, us1m voltage drop
- Vegetables sklearn - xgboost (2)
- Part 33 of SAP ui5 application development tutorial - trial version of responsiveness of SAP ui5 applications
- What changes have taken place in the project file after SAP ui5 tools ran the Fiori add deploy config command
猜你喜欢

No one reads the series. Source code analysis of copyonwritearraylist
MySQL transaction learning notes (I) first encounter

Uni app wechat applet customer service chat function

50 days countdown! Are you ready for the Landbridge cup provincial tournament?

Hands on deep learning (III)

General test point ideas are summarized and shared, which can be directly used in interview and actual software testing

【LeetCode】40. Combined summation II (2 strokes of wrong questions)

Wireless industrial Internet of things data monitoring terminal

IQ debugging of Hisilicon platform ISP and image (1)

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
随机推荐
[kicad image] download and installation
SAP ui5 beginner tutorial No. 28 - Introduction to the integration test tool OPA for SAP ui5 applications
Ifconfig command – displays or sets network devices
Go uses channel to control concurrency
Socket, network model notes
[road of system analyst] collection of wrong questions in the chapters of Applied Mathematics and economic management
MySQL tuning --01--- optimization steps and system performance parameters
Noi Mathematics: Dirichlet convolution
Technology inventory: past, present and future of Message Oriented Middleware
Uname command – displays system information
[network security] sharing of experience and ideas of an emergency battle
Leetcode sword finger offer question brushing - day 27
How SAP ui5 device type detection device API works
[data visualization application] draw spatial map (with R language code)
Day21 performance test process
Research Report on demand and Competitive Prospect of global and Chinese welding personal protective equipment industry 2022-2027
Interviewer: what is an iterator? What is the relationship between async await and iterators?
Report on strategic suggestions on investment direction and Prospect of global and Chinese marine biological industry (2022 Edition)
C switch nested syntax
At the age of 26, I was transferred to software testing with zero foundation. Now I have successfully entered the job with a monthly salary of 12K. However, no one understands my bitterness