Code Monkey home page Code Monkey logo

thinksocketio's Introduction

简介

一个使用PHPSocket.IOThinkPHP5.1,以及JQ实现的聊天室,包括功能有:

  • 公频广场聊天
  • 自定义用户名
  • 一对一私聊
  • 公频中私聊信息提醒
  • 新用户加入以及离开提醒
  • 在线用户列表,人数统计
  • 系统主动推送广播,或向指定用户推送消息

截图

广场公频聊天: 广场公频聊天 一对一私聊: 一对一私聊 广播推送系统消息: 广播推送系统消息 指定用户推送消息: 指定用户推送消息

使用说明

以下说明假定已经为项目public目录绑定了域名test.com并正确部署服务器,访问test.com能看到TP5默认首页。

如果修改了配置或者代码,请重新运行服务端,否则代码不生效。

安装

git clone [email protected]:hsu1943/thinksocketio.git
cd thinksocketio
composer install -vvv

配置

模板文件/application/socketio/view/index/index.html/application/socketio/view/index/chat.htmlsocket修改为你的socket服务端地址,默认测试端口2021,端口号可以在/config/socketio/param.php中配置。这里是本机测试,用http://127.0.0.1:2021

var socket = io('http://127.0.0.1:2021');

使用数据库记录消息:

如果你需要使用数据库存储消息,按照下面的步骤打开配置,打开配置后系统会根据昵称将所有聊天记录写入数据库msg表中,包括主动推送的消息;

  1. config/database.php中配置数据库,保证数据库能正确连接;
  2. 导入根目录下msg.sql到数据库;
  3. config/socketio/param.php中将save_msg修改为true(默认是false,不写入到数据库);

这里说明一下,请保证数据库能正确连接并且里面有正确的表结构(第二步导入表)再进行第三步配置。

主动推送系统消息接口

修改配置文件/config/socketio/param.php中的配置为监听消息推送地址,这里本地测试,使用本地2121端口;

return [
    'ws' => [
        'apiHost' => 'http://127.0.0.1:2121',
    ],
];

该地址即系统推送消息地址,参数:

to:接收人
content:消息

两种用法:

  1. 其他项目POST或GET请求接口即可推送消息
向username推送系统消息
http://test.com/system?to=username&content=系统推送消息测试
广播消息
http://test.com/system?content=系统推送消息测试

http://test.com/system这是本项目使用上面的监听地址做的一个消息推送demo,详情看源代码。

  1. 本项目中推送系统消息:

已将推送封装在Msg的模型中,使用:

$res = Msg::send($to, $content);
return $res == 'ok' ? '系统消息推送成功' : '系统消息推送失败';

测试

运行服务端:

php ./public/server.php

这里可以将输出写到某个日志文件中,或者使用supervisor来管理服务端。

访问以下地址即可进入公频:

http://test.com/socketio

点击消息列表中的用户名即可进入私聊。

更新

  • 2019-12-19 增加服务端的输出,以及端口配置项,保存数据库加上错误处理,更新项目README;
  • 2019-06-19 增加在线人数统计,在线用户列表,修改昵称,添加系统主动推送接口(广播或私信);

开发记录

以下两篇文章是在开发过程中的记录,代码不是最新,最新代码以本项目github为准,有问题可以去文章里留言。

ThinkPHP 5.1+PHPSocket.IO实现websocket搭建聊天室+私聊

ThinkPHP 5.1下使用PHPSocket.IO实现websocket通讯

感谢

thinksocketio's People

Contributors

hsu1943 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.