WebSocket私信聊天(无群聊天)

//php 后端  
<?php
$server = new Swoole\WebSocket\Server("0.0.0.0", 9501);

$server->on('open', function (Swoole\WebSocket\Server $server, $request) {
    //$server->push($request->fd, '欢迎进入我们的聊天室~');
});

//接收客户端发送的信息
//$frame 是 Swoole\WebSocket\Frame 对象,包含了客户端发来的数据帧信息
//$frame->fd 代表客户端的唯一标识 客户id
//$frame->data  代表客户端发送的消息
$server->on('message', function (Swoole\WebSocket\Server $server, $frame) {

    //获取客户端发送的消息
    $type = json_decode($frame->data,true);
    if($type['type'] != 'active'){
        $result['data'] = $frame->data;
        $types = 1;
        $framedata = json_decode($frame->data,true);
        $openid  = $framedata['openid'];
        $endopenid  = $framedata['endopenid'];
        $imgtype  = $framedata['imgtype'];
        $url = 'http://www.xxx.com/';
        $wxapp = '/app/index.php?i=2&t=0&v=1.01&from=wxapp&c=entry&a=wxapp';
        // 会话列表创建会话接口
        $socket_add = '&do=chat_socket_add&m=gc_school&';
        //会话信息查询接口
        $socket_find = '&do=chat_socket_find&m=gc_school&';
        //用户openid 绑定会话 fd 接口
        $user_fid = '&do=chat_user_find&m=gc_school&';
        //存储消息记录
        $socket_log = '&do=socket_log&m=gc_school&';
        //重置fd为0
        $socket_isfd = '&do=socket_isfd&m=gc_school&';
        //查询对方的fd
        $user_endfind = '&do=chat_user_endfind&m=gc_school&';
        //查询用户信息
        $Socket_users = '&do=Socket_users&m=gc_school&';

        //如果不存在会话id 就去创建会话ID

// var_dump( f r a m e − > r e l a t i o n ) ; d i e ; i f ( frame->relation);die; if( frame>relation);die;if(framedata[‘relation’]){
u s e s s = ′ i d s = ′ . usess = 'ids='. usess=ids=.framedata[‘relation’];
//如果存在会话id就去查询会话id的信息
$relation_url = u r l . url. url.wxapp. s o c k e t f i n d . socket_find. socketfind.usess;
r e l a t i o n d a t a = f i l e g e t c o n t e n t s ( relationdata = file_get_contents( relationdata=filegetcontents(relation_url); // 查询会话信息
}else{
u s e s s = ′ o p e n i d = ′ . usess = 'openid='. usess=openid=.openid.‘&endopenid=’.$endopenid;
//拼接创建会话id信息并请求接口
$relation_url = u r l . url. url.wxapp. s o c k e t a d d . socket_add. socketadd.usess;
r e l a t i o n d a t a = f i l e g e t c o n t e n t s ( relationdata = file_get_contents(

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大白d98

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值