[李景山php]Swoole 编写 IM通信
来源:互联网 发布:北京东京 知乎 编辑:程序博客网 时间:2024/05/16 19:51
Swoole 编写 IM通信
服务器端口:
//创建websocket服务器对象,监听0.0.0.0:9502端口$ws = new swoole_websocket_server("0.0.0.0", 9502);//监听WebSocket连接打开事件$ws->on('open', function ($ws, $request) {$GLOBALS['fd'][$request->fd]['id'] = $request->fd;// 设置用户ID 安顺序递增$GLOBALS['fd'][$request->fd]['name'] = '匿名用户';// 设置用户名});//监听WebSocket消息事件$ws->on('message', function ($ws, $frame) {$msg = $GLOBALS['fd'][$frame->fd]['name'].":{$frame->data}\n";if(strstr($frame->data,'#name#')){// 用户设置昵称$GLOBALS['fd'][$frame->fd]['name'] = str_replace('#name#','',$frame->data);}else{// 普通发送用户信息foreach($GLOBALS['fd'] as $i){// 发送数据到客户端$ws->push($i['id'],$msg);}}});//监听WebSocket连接关闭事件$ws->on('close', function ($ws, $fd) {echo "客户端-{$fd} 断开连接\n";unset($GLOBALS['fd'][$fd]);// 清除 已经关闭的客户端});$ws->start();
客户端关键代码:
/** * Created by 27394 on 2017/4/24. */var msg = document.getElementById("msg");var wsServer = 'ws://192.168.50.151:9502';//调用websocket对象建立连接://参数:ws/wss(加密)://ip:port (字符串)var websocket = new WebSocket(wsServer);websocket.onopen = function (evt) {//onopen监听连接打开// 应该显示远程服务器连接成功//msg.innerHTML = websocket.readyState;//websocket.readyState 属性:/* CONNECTING 0The connection is not yet open. OPEN 1The connection is open and ready to communicate. CLOSING2The connection is in the process of closing. CLOSED 3The connection is closed or couldn't be opened. */};//onmessage 监听服务器数据推送websocket.onmessage = function (evt) {msg.innerHTML += evt.data +'<br>';//不断递增的数据console.log('从服务器获取到的数据: ' + evt.data);};//监听连接关闭websocket.onclose = function (evt) {console.log("服务器拒绝");};//监听连接错误信息websocket.onerror = function (evt, e) {console.log('错误: ' + evt.data);};//发送信息function send_msg(){var text = document.getElementById('text').value;// 获取数据document.getElementById('text').value = '';// 清空数据websocket.send(text);//向服务器发送数据}//发送昵称function send_name(){var text = document.getElementById('myname').value;// 获取数据websocket.send("#name#"+text);//向服务器发送数据var myTitle = document.getElementById("myTitle");myTitle.innerHTML = "IM "+text;alert("设置成功");var setName = document.getElementById("setName");setName.style.display = "none";var send_msg = document.getElementById("send_msg");send_msg.style.display = "block";}
0 0
- [李景山php]Swoole 编写 IM通信
- [李景山php] swoole 进程间 通信
- PHP+swoole+websocket聊天实时通信
- [李景山php] swoole 之 定时器
- [李景山php] swoole tcp 客户端
- [李景山php] swoole 进程创建
- [李景山php] swoole 锁机制
- [李景山php] swoole dns 查询
- [李景山php] swoole 事件添加
- PHP--swoole
- Swoole,PHP语言的高性能网络通信框架
- 【Swoole】当SWOOLE遇上PHP
- 【Swoole】当SWOOLE遇上PHP
- [李景山php]swoole学习-centos7安装
- [李景山php] swoole使用 常用案例
- [李景山php] swoole系列实用案例-TCPserver
- [李景山php] swoole之 UDP服务器实现
- [李景山php] swoole 之web服务器
- PLC作业(119-120)
- python 读取文件时报错UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 205: illegal multib
- linux curl 上传文件
- java调用阿里云实现短信推送
- 我的博客开通了,欢迎大家随时来
- [李景山php]Swoole 编写 IM通信
- bug宝典Linux篇 ALERT] 121/164316 (38066) : parsing [/etc/haproxy/haproxy.cfg:56] : 'bind *:9091' unknow
- linux基础之文本处理工具
- Win10命令行下对MySQL的一些基本操作
- 算法解析之K-近邻算法数学分析
- websocket实现语音通讯
- 2017-05 JS笔记
- C#.net 关键字之new老司机教学 net new
- ZJU2058The Archaeologist's Trouble II