基於swoole的聊天室模型

client.html:    

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no"/>
<title>聊天室</title>
<script src="https://code.jquery.com/jquery-1.11.3.js" type="text/javascript"></script>
<script src="swoole.js" type="text/javascript"></script>
<style>
body {
padding: 0;
margin: 0;
font-size: 12px;
}

div.form {
width: 100%;
border: 1px solid #abc;
}

ul.body {
width: 100%;
}

ul.body li {
clear: both;
width: 100%;
line-height: 2em;
}
</style>
</head>
<body>
<div class="form">
<input type="text" id="msg" size="100">
<input type="button" id="send" value="waiting">
</div>
<ul class="body">

</ul>
</body>
</html>













swoole.js

var cSetting = {
ip: '192.168.1.11',
port: 5002 //這兒的端口要與下面swoole.php中的端口相同,且要在防火牆中打開此端口。
};
var chat = {
Socket: null,
init: function () {
var self = this;
self.Socket = new WebSocket('ws://' + cSetting.ip + ':' + cSetting.port);
self.Socket.onopen = function (e) {
if (self.Socket.readyState != 1) return;
$('input#send').val('Send');
self.Socket.send('hello');
};
self.Socket.error = function (e) {
self.Socket = false;
};
self.Socket.onclose = function (e) {
self.print('斷網啦');
self.Socket = false;
};
self.Socket.onmessage = function (request) {
self.print(request.data);
};
},

send: function ($val) {
if (this.Socket == null)this.init();
if (!this.Socket)return;
this.Socket.send($val);
},
close: function () {
if (!this.Socket)return;
this.Socket.close();
},
print: function ($str) {
$('ul.body').append('<li>' + $str + '</li>');
}
};
$(function () {

chat.init();
$('input#send').on('click', function () {
var input = $('input#msg');
chat.send(input.val());
input.val('');
});

$(window).bind('beforeunload', function () {
chat.close();
});


});






swoole.php    【在linux中以cli運行:例:php -f /home/test/swoole.php 】

<?phpservice(5002);function service($port = 9501){    $setting = [        'user' => 'www',    //子進程運行用戶名        'group' => 'www',    //子進程運行用戶名所在組//            'daemonize' => 1,    //守護進程化。        'log_file' => __DIR__ . '/log1.log',    //指定swoole錯誤日誌文件。        'max_conn' => 10000,   //最大容許的鏈接數,        'reactor_num' => 4,    //線程數,爲CPU核數的1-4倍,建議2倍,最大不得超過CPU核數*4。        'worker_num' => 4,    //進程數。        'max_request' => 50,//每一個進程的最大任務數        'task_worker_num' => 2,   //配置task進程的數量,配置此參數後將會啓用task功能。因此swoole_server務必要註冊onTask/onFinish2個事件回調函數。若是沒有註冊,服務器程序將沒法啓動。,        'task_ipc_mode' => 1,   //設置task進程與worker進程之間通訊的方式,1, 使用unix socket通訊 2, 使用消息隊列通訊 3, 使用消息隊列通訊,並設置爲爭搶模式 設置爲3後,task/taskwait將沒法指定目標進程ID        'task_max_request' => 0,   //設置task進程的最大任務數,        'task_tmpdir' => __DIR__ . '/',   //設置task的數據臨時目錄        'discard_timeout_request' => true,   //丟棄已失聯進程收到的數據請求        'backlog' => 128,   //listen backlog    ];    $socket = new swoole_websocket_server("0.0.0.0", $port);    $socket->addlistener('0.0.0.0', $port + 1, SWOOLE_SOCK_TCP);    $socket->set($setting);    $socket->on('start', function (swoole_websocket_server $server) {        echo "Service:\n================================\n";        print_r(['master_pid' => $server->master_pid, 'manager_pid' => $server->manager_pid]);    });    $socket->on('open', function (swoole_websocket_server $server, swoole_http_request $request) {        echo "open:" . print_r($request, true) . "\n";    });    $socket->on('message', function (swoole_websocket_server $server, $request) {        echo "message:" . print_r($request, true) . "\n";        $val = $request->data;        $id = $request->fd;        if (substr($val, 0, 4) === 'send') {            var_dump($server->send($id, '【' . date('H:i:s') . '】Message Send: ' . $val));        } else if (substr($val, 0, 4) === 'task') {            $server->push($id, '【' . date('H:i:s') . '】Message Push: ' . $val . '[' . mt_rand() . ']');            $server->task([$id, $val]);        } else {            $server->push($id, '【' . date('H:i:s') . '】Message Push: ' . $val . '[' . mt_rand() . ']');        }    });    $socket->on('task', function (swoole_websocket_server $server, $task_id, $from_id, $key) {        echo "task:" . "\t taskID={$task_id}\t fromID={$from_id}\n" . print_r($key, true);        sleep(1);        $server->push($key[0], '後臺回覆:' . $key[1]);        $server->finish($key[0]);    });    $socket->on('finish', function (swoole_websocket_server $server, $task_id, $data) {        echo "Finish taskID={$task_id}\n";    });    $socket->on('close', function (swoole_websocket_server $server, $fd) {        echo "close:" . print_r($fd, true) . "\n";    });    $socket->start();}
相關文章
相關標籤/搜索