ThinkPhP5+socket 实现网页聊天(附源码)

Windows环境下运行:

在目录chat\vendor\GatewayWorker-for-win下双击打开 start_for_win.bat

ThinkPhP5+socket 实现网页聊天(附源码)

  1. 配置好数据库
  2. http://chat.com 指向到 项目的public目录
  3. chat\application\index\view\index\index.html文件中的 API_URL = "http://chat.com/index.php/api/chat/";
  4. chat\application\index\view\index\index.html文件中的 ws = new WebSocket("ws://127.0.0.1:8282");

然后打开连接:
小明: http://chat.com/index.php/index/index/index?fromid=87&toid=85
小李: http://chat.com/index.php/index/index/index?fromid=85&toid=87
如下所示:

ThinkPhP5+socket 实现网页聊天(附源码)

就可以直接聊天了。

Linux 环境搭建项目:

在目录chat\vendor\GatewayWorker-for-win下执行 php start.php start 即可。

  1. 配置好数据库
  2. http://xxx.com 指向到 项目的public目录
  3. chat\application\index\view\index\index.html文件中的 API_URL = "http://xxx.com/index.php/api/chat/";
  4. chat\application\index\view\index\index.html文件中的 ws = new WebSocket("ws://ip:8282");

需要注意的是第三步第四步需要更换成自己服务器的域名跟ip,没有换就会报跨域错误。另外需要去安全组开启8282端口,然后再到服务端监听8282端口,如果用了面板,比如宝塔,需要放行8282端口。

源码跟数据库传送门