WebSocket\Server

1.7.9增加了内置的WebSocket服务器支持,通过几行PHP代码就可以写出一个异步非阻塞多进程的WebSocket服务器。

  1. $server = new Swoole\WebSocket\Server("0.0.0.0", 9501);
  2. $server->on('open', function (Swoole\WebSocket\Server $server, $request) {
  3. echo "server: handshake success with fd{$request->fd}\n";
  4. });
  5. $server->on('message', function (Swoole\WebSocket\Server $server, $frame) {
  6. echo "receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish}\n";
  7. $server->push($frame->fd, "this is server");
  8. });
  9. $server->on('close', function ($ser, $fd) {
  10. echo "client {$fd} closed\n";
  11. });
  12. $server->start();

onRequest回调

WebSocket\Server 继承自 Http\Server

  • 设置了onRequest回调,WebSocket\Server也可以同时作为http服务器
  • 未设置onRequest回调,WebSocket\Server收到http请求后会返回http 400错误页面
  • 如果想通过接收http触发所有websocket的推送,需要注意作用域的问题,面向过程请使用globalWebSocket\Server进行引用,面向对象可以把WebSocket\Server设置成一个成员属性
    1、面向过程代码
  1. $server = new Swoole\WebSocket\Server("0.0.0.0", 9501);
  2. $server->on('open', function (Swoole\WebSocket\Server $server, $request) {
  3. echo "server: handshake success with fd{$request->fd}\n";
  4. });
  5. $server->on('message', function (Swoole\WebSocket\Server $server, $frame) {
  6. echo "receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish}\n";
  7. $server->push($frame->fd, "this is server");
  8. });
  9. $server->on('close', function ($ser, $fd) {
  10. echo "client {$fd} closed\n";
  11. });
  12. $server->on('request', function (Swoole\Http\Request $request, Swoole\Http\Response $response) {
  13. global $server;//调用外部的server
  14. // $server->connections 遍历所有websocket连接用户的fd,给所有用户推送
  15. foreach ($server->connections as $fd) {
  16. $server->push($fd, $request->get['message']);
  17. }
  18. });
  19. $server->start();

2、面向对象代码

  1. class WebsocketTest {
  2. public $server;
  3. public function __construct() {
  4. $this->server = new Swoole\WebSocket\Server("0.0.0.0", 9501);
  5. $this->server->on('open', function (swoole_websocket_server $server, $request) {
  6. echo "server: handshake success with fd{$request->fd}\n";
  7. });
  8. $this->server->on('message', function (Swoole\WebSocket\Server $server, $frame) {
  9. echo "receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish}\n";
  10. $server->push($frame->fd, "this is server");
  11. });
  12. $this->server->on('close', function ($ser, $fd) {
  13. echo "client {$fd} closed\n";
  14. });
  15. $this->server->on('request', function ($request, $response) {
  16. // 接收http请求从get获取message参数的值,给用户推送
  17. // $this->server->connections 遍历所有websocket连接用户的fd,给所有用户推送
  18. foreach ($this->server->connections as $fd) {
  19. $this->server->push($fd, $request->get['message']);
  20. }
  21. });
  22. $this->server->start();
  23. }
  24. }
  25. new WebsocketTest();

客户端

  • Chrome/Firefox/高版本IE/Safari等浏览器内置了JS语言的WebSocket客户端
  • 微信小程序开发框架内置的WebSocket客户端
  • 异步的PHP程序中可以使用Swoole\Http\Client作为WebSocket客户端
  • apache/php-fpm或其他同步阻塞的PHP程序中可以使用swoole/framework提供的同步WebSocket客户端
  • WebSocket客户端不能与WebSocket服务器通信