Home  >  Article  >  Backend Development  >  Use PHP's swoole framework to implement high-performance long connection websocket

Use PHP's swoole framework to implement high-performance long connection websocket

WBOY
WBOYOriginal
2016-07-29 09:14:391877browse

Swoole’s official website please visit: http://www.swoole.com/
swoole-git: https://github.com/swoole/swoole-src

1. Server side:

1. Edit the server.php file content as follows:

<code><span><span><?php</span><span>$reqs</span>=<span>array</span>(); <span>//保持客户端的长连接在这个数组里</span><span>$serv</span> = <span>new</span> swoole_websocket_server(<span>"192.168.2.115"</span>, <span>9502</span>);
<span>//如下可以设置多端口监听</span><span>//$server = new swoole_websocket_server("0.0.0.0", 9501, SWOOLE_BASE);</span><span>//$server->addlistener('0.0.0.0', 9502, SWOOLE_SOCK_UDP);</span><span>//$server->set(['worker_num' => 4]);</span><span>$serv</span>->on(<span>'Open'</span>, <span><span>function</span><span>(<span>$server</span>, <span>$req</span>)</span> {</span><span>global</span><span>$reqs</span>;
    <span>$reqs</span>[]=<span>$req</span>->fd;
    <span>echo</span><span>"connection open: "</span>.<span>$req</span>->fd.<span>"\n"</span>;
    var_dump(count(<span>$reqs</span>));<span>//输出长连接数</span>
});

<span>$serv</span>->on(<span>'Message'</span>, <span><span>function</span><span>(<span>$server</span>, <span>$frame</span>)</span> {</span><span>global</span><span>$reqs</span>;
    <span>echo</span><span>"message: "</span>.<span>$frame</span>->data.<span>"\n"</span>;
    <span>foreach</span>(<span>$reqs</span><span>as</span><span>$fd</span>){
            <span>$server</span>->push(<span>$fd</span>, <span>$frame</span>->data);
    }
});

<span>$serv</span>->on(<span>'Close'</span>, <span><span>function</span><span>(<span>$server</span>, <span>$fd</span>)</span> {</span><span>echo</span><span>"connection close: "</span>.<span>$fd</span>.<span>"\n"</span>;
});

<span>$serv</span>->start();</span></code>

2. Start the websocket service code implemented above

<code>[songaimin<span>@localhost</span><span>Tests</span>]<span>$/</span>usr/bin/php server.php</code>

Client - Colleagues open two browsers to simulate multiple clients:

3. Run in the browser console:

<code>ws = <span>new</span> WebSocket(wsl);<span>//新建立一个连接</span><span>//如下指定事件处理 </span>
ws.onopen = <span><span>function</span><span>()</span>{</span>ws.send(<span>'Test!'</span>); };  
ws.onmessage = <span><span>function</span><span>(evt)</span>{</span>console.log(evt.data);<span>/*ws.close();*/</span>};  
ws.onclose = <span><span>function</span><span>(evt)</span>{</span>console.log(<span>'WebSocketClosed!'</span>);};  
ws.onerror = <span><span>function</span><span>(evt)</span>{</span>console.log(<span>'WebSocketError!'</span>);}; </code>

4. Manually execute in any browser console:

<code>ws.send<span>(888</span>);
<span>//看每个浏览器的内容console输出应该是一样的就实验在成功了</span>
ws.<span>close</span>();<span>//关闭连接</span></code>

Related recommended articles:
Real-time chat room implemented by swoole http://segmentfault.com/a/1190000003057118

The above introduces the use of PHP's swoole framework to achieve high-performance long-connection websocket, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn