Flask接口签名sign原理与实例代码浅析
243
2022-10-31
swoole 初试教程 Im聊天
代码 swoole_im.php
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();
html js css 上线链接吧
主要的webscoket的js
/** * Created by PhpStorm. * User: Administrator * Date: 2018/12/27 * Time: 11:46 */var msg = document.getElementById("msg");var wsServer = 'ws://192.168.244.132:9508';//调用websocket对象建立连接://参数:ws/wss(加密)://ip:port (字符串)var websocket = new WebSocket(wsServer);websocket.onopen = function (evt) {//onopen监听连接打开 console.log("连接成功"); // 应该显示远程服务器连接成功 //msg.innerHTML = websocket.readyState; //websocket.readyState 属性: /* CONNECTING 0 The connection is not yet open. OPEN 1 The connection is open and ready to communicate. CLOSING 2 The connection is in the process of closing. CLOSED 3 The connection is closed or couldn't be opened. */};//onmessage 监听服务器数据推送websocket.onmessage = function (evt) { msg.innerHTML += evt.data +'
';//不断递增的数据 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;// 获取数据 console.log(text); 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";}
结果
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~