代码
https://github.com/7117/Graphic-live-broadcasting-site/tree/master/swoole%20example/chatroom
数据结构
# 公聊结构
{
"chattype":"publicchat",
"chatto":"0",
"chatmsg":"具体的聊天逻辑"
}
# 私聊结构
{
"chattype":"privatechat",
"chatto":"2614677",
"chatmsg":"具体的聊天逻辑"
}
页面
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>websocket client</title>
<style type="text/css">
.container {
border: #ccc solid 1px;
}
.up {
width: 100%;
height: 200px;
}
.down {
width: 100%;
height: 100px;
}
</style>
</head>
<body>
<div class="container">
<div class="up" id="chatrecord">
</div>
<hr>
<div class="down">
聊天类型:
<select id="chattype">
<option value="publicchat">公聊</option>
<option value="privatechat">私聊</option>
</select>
对
<select id="chatto">
</select>
说:<input type="text" id="chatmsg" placeholder="聊聊天">
<input type="button" id="btnsend" value="发送">
</div>
</div>
</body>
<script src="http://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
var ws;
ws = new WebSocket("ws://chat.room.com:8811");
ws.onopen = function (evt) {
if (ws.readyState == 1) {
$("#chatrecord").append("<p>" + '这是来自客户端的欢迎' + "</p>");
}
}
ws.onmessage = function (event) {
var data = $.parseJSON(event.data);
$("#chatrecord").append("<p>" + data.msg + "</p>");
console.log(data.total.length);
console.log($("#chatto option").length);
$(data.total).each(function (k, v) {
if( data.total.length > $("#chatto option").length){
$("#chatto").append("<option id='k'>" + v + "</option>");
}
})
}
ws.onclose = function (event) {
$("#chatrecord").append("<p>" + 关闭 + "</p>");
}
ws.onerror = function (event) {
$("#chatrecord").append("<p>" + event.data + "</p>");
}
$("#btnsend").click(function sendMsg() {
var chatmsg = $("#chatmsg").val();
var chattype = $("#chattype").val();
var chatto = $("#chatto").val();
var msg = JSON.stringify({"chattype": chattype, "chatto": chatto, "chatmsg": chatmsg});
if (msg != "" && chatmsg != "") {
ws.send(msg);
$("#chatmsg").val("");
}
})
})
</script>
</html>
消息处理
<?php
/**
* 用于实现公聊私聊的特定发送服务。
* */
class Dispatcher
{
const CHAT_TYPE_PUBLIC = "publicchat";
const CHAT_TYPE_PRIVATE = "privatechat";
public $frame = '';
public $clientid = '';
public $chatData = '';
public function __construct($frame)
{
$this->frame = $frame;
$this->clientid = intval($this->frame->fd);
print_r($frame);
}
public function getChatData()
{
$frameData = $this->frame->data;
if ($frameData) {
$frameData = json_decode($frameData, true);
$this->chatData = $frameData;
return $this->chatData;
}
}
public function getSenderId()
{
return $this->clientid;
}
public function getReceiverId()
{
return intval($this->chatData['chatto']);
}
public function isPrivateChat()
{
$chatdata = $this->getChatData();
return $chatdata['chattype'] == self::CHAT_TYPE_PUBLIC ? false : true;
}
public function sendPrivateChat($server, $toid, $msg)
{
foreach ($server->connections as $key => $fd) {
if ($toid == $fd || $this->clientid == $fd) {
$info = [
'msg' => $msg,
];
$server->push($fd, json_encode($info));
}
}
return;
}
public function sendToEvery($server, $msg)
{
$total = $server->getClientList();
foreach ($server->connections as $key => $fd) {
$info = [
'msg' => $msg,
'total' => $total
];
$server->push($fd, json_encode($info));
}
return;
}
}
服务器
<?php
include "./dispatcher.php";
class Server
{
public function __construct()
{
error_reporting(E_ALL & ~E_WARNING & ~E_NOTICE);
$ws = new swoole_websocket_server("0.0.0.0", 8811);
//设置静态页
$ws->set([
'enable_static_handler' => true,
'document_root' => "./",
'worker_num' => 5
]);
$ws->on("open", function ($ws, $request) {
echo "open:client {$request->fd}" . PHP_EOL;
$count = count($ws->connections);
//获取所有的连接 进行遍历展示
$totalConn = $ws->getClientList();
foreach ($ws->connections as $key => $fd) {
$welcomeWord = "";
$info = [
'msg' => $welcomeWord,
'total' => $totalConn
];
$ws->push($fd, json_encode($info));
}
});
// $frame 是 swoole_websocket_frame 对象,包含了客户端发来的数据帧信息
$ws->on("message", function ($ws, $frame) {
//接收客户端的信息
$dispatcher = new Dispatcher($frame);
//获取
$chatdata = $dispatcher->getChatData();
$fromid = $dispatcher->getSenderId();
$toid = $dispatcher->getReceiverId();
$isprivatechat = $dispatcher->isPrivateChat();
//私聊
if ($isprivatechat) {
$msg = "【{$fromid}】对【{$toid}】说:{$chatdata['chatmsg']}";
$dispatcher->sendPrivateChat($ws, $toid, $msg);
//公聊
} else {
$msg = "【{$fromid}】对大家说:{$chatdata['chatmsg']}";
$dispatcher->sendToEvery($ws, $msg);
}
});
$ws->on("close", function ($ws, $fd) {
if ($ws->isEstablished) {
foreach ($ws->connections as $key => $fd) {
$goodbyeMag = "CLOSE:Client {$fd} leave this chat room.";
$info = [
'msg' => $goodbyeMag,
];
$ws->push($fd, json_encode($info));
}
}
});
$ws->start();
}
}
$ws = new Server();