前提:
-
linux环境下PHP有可用的Swoole扩展。
-
9501端口可访问。
后端
<?php
class Helper {
public static function nullToEmptyString($arr) {
if (! $arr) {
return $arr;
}
foreach ($arr as $key => $value) {
if (null === $value) {
$arr[$key] = '';
}
if (is_array($value)) {
$arr[$key] = static::nullToEmptyString($value);
}
}
return $arr;
}
public static function jsonReturn($code = 0, $msg = '', $data = []) {
header('Content-type:text/json;Charset=UTF-8');
return json_encode([
'code' => $code,
'msg' => $msg,
'data' => $data === null ? [] : static::nullToEmptyString($data),
], JSON_UNESCAPED_UNICODE);
}
public static function setPersonNumber($num) {
return file_put_contents(__DIR__ . '/personNum.txt', $num);
}
public static function getPersonNumber() {
$file = __DIR__ . '/personNum.txt';
if(! file_exists($file)) {
return 0;
}
return file_get_contents($file);
}
public static function broadcast(Swoole\WebSocket\Server $ws, $msg) {
$fd = static::getPersonNumber();
if($fd > 0) {
for($i = 1; $i <= $fd; $i++) {
$ws->push($i, $msg);
}
}
return null;
}
}
$ws = new Swoole\WebSocket\Server('0.0.0.0', 9501);
$ws->on('Open', function ($ws, $request) {
$ws->nickname = '游客'. $request->fd;
Helper::setPersonNumber($request->fd);
Helper::broadcast($ws, Helper::jsonReturn(0, '', ['type' => 'enter', 'user' => $ws->nickname, 'content' => '']));
return null;
});
$ws->on('Message', function ($ws, $frame) {
Helper::broadcast($ws, Helper::jsonReturn(0, '', ['type' => 'say', 'user' => $ws->nickname, 'content' => htmlspecialchars($frame->data)]));
return null;
});
$ws->on('Close', function ($ws, $fd) {
Helper::setPersonNumber(-- $fd);
Helper::broadcast($ws, Helper::jsonReturn(0, '', ['type' => 'leave', 'user' => $ws->nickname, 'content' => '']));
return null;
});
$ws->start();
前端
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>简易聊天室</title>
<style>
body * {font-size: 20px;width: 400px;height: 40px;}
</style>
</head>
<body>
<input type="text" id="text">
<input type="button" id="send" value="发送">
<div id="box"></div>
</body>
<script>
var websocket = new WebSocket('ws://192.168.3.180:9501/swoole.php');
websocket.onopen = function (evt) {
document.getElementById('send').onclick = function() {
var msg = document.getElementById('text').value;
if(msg) {
websocket.send(msg);
}
}
};
websocket.onmessage = function (evt) {
var data = JSON.parse(evt.data);
var data = data.data;
if(data.type == 'enter') {
var e = '<p style="color:green">' + data.user + '进入聊天室</p>';
} else if(data.type == 'leave') {
var e = '<p style="color:red">' + data.user + '离开聊天室</p>';
} else if (data.type == 'say') {
var e = '<p style="color:black">' + data.user + '说:' + data.content + '</p>';
}
var element = document.getElementById('box').innerHTML += e;
};
websocket.onclose = function (evt) {
console.log("WebSocket 已断开连接");
};
websocket.onerror = function (evt, e) {
console.log("WebSocket 错误:" + evt.data);
};
</script>
</html>
出处:https://www.cnblogs.com/phpphp/p/17501368.html