php websocket
生活随笔
收集整理的這篇文章主要介紹了
php websocket
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
php websocket項目開發,推薦使用:Workerman
本片內容使用Workerman實現了簡單的及時聊天功能,具體代碼如下:
<?php // phpinfo(); header('Content-Type: text/html; charset=utf-8');require 'vendor/autoload.php';use Workerman\Worker;$sk=new Sock();//對創建的socket循環進行監聽,處理數據 $sk->run();function array_remove($arr, $key){ if(!array_key_exists($key, $arr)){ return $arr; } $keys = array_keys($arr); $index = array_search($key, $keys); if($index !== FALSE){ array_splice($arr, $index, 1); } return $arr; } class Sock{public $sockets; //socket的連接池,即client連接進來的socket標志public $ws_worker;public function __construct(){// Create a Websocket server$this->ws_worker = new Worker("websocket://0.0.0.0:8889");// 4 processes$this->ws_worker->count = 4;// Emitted when new connection come$this->ws_worker->onConnect = function($connection){echo "New connection\n";echo 'id=' . $connection->id . ' ';$this->sockets[$connection->id] = array('client'=>$connection);echo 'count=' . count($this->sockets) . ' ';};// Emitted when data received$this->ws_worker->onMessage = function($connection, $data){// Send hello $dataecho "\n".$connection->id." -> req: ".$data;$jdata = json_decode($data,true);echo "\n op: ".$jdata['op'];if($jdata['op'] == 'login'){//{'op':'login','user':user}$cs = $this->sockets[$connection->id];$cs['user'] = $jdata['user'];$this->sockets[$connection->id] = $cs;$connection->send($jdata['user'].'登錄成功'); return;}else{// {'op':'chat','from_user':user,'to_user':user,'msg':msg}$deal = false;if($jdata['op'] == 'chat'){foreach ($this->sockets as $key => $value) {if($value['user'] == $jdata['to_user']){$value['client']->send($jdata['msg']);$connection->send($data);$deal = true;}}if($deal == false){$connection->send($jdata['user'].'會員不存在');}}else{$connection->send('參數異常: ' . $data); } }};// Emitted when connection closed$this->ws_worker->onClose = function($connection){echo "Connection closed\n";$this->sockets = array_remove($this->sockets, $connection->id);};}public function run(){Worker::runAll(); } } <!DOCTYPE html> <html> <head><meta charset="UTF-8"><title></title><script src="jquery.js" type="text/javascript"></script></head> <body><input type="text" id="input" placeholder="Message…" /><hr /><pre id="output"></pre><script>var user = '';var host = 'ws://127.0.0.1:8889';var socket = null;var input = document.getElementById('input');var output = document.getElementById('output');var print = function (message) {var samp = document.createElement('samp');samp.innerHTML = message + '\n';output.appendChild(samp);return;};user = window.prompt("歡迎?","請在此輸入您的姓名。");input.addEventListener('keyup', function (evt) {if (13 === evt.keyCode) {var msg = input.value;if (!msg) {return;}try {socket.send(msg);input.value = '';input.focus();} catch (e) {console.log(e);}return;}});try {socket = new WebSocket(host);socket.onopen = function () {print('connection is opened');input.focus();socket.send('{"op":"login","user":"'+user+'"}');return;};socket.onmessage = function (msg) {print(msg.data);return;};socket.onclose = function () {print('connection is closed');return;};} catch (e) {console.log(e);}</script> </body> </html>?
使用 php socket.php 啟動服務端。
js端要發起json結構的數據,如下截圖:
?
轉載于:https://www.cnblogs.com/scott-j/p/8995940.html
總結
以上是生活随笔為你收集整理的php websocket的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python小游戏之 - 飞机大战 !
- 下一篇: dijkstra 的优先队列优化