代码实现(域名分http/https两种情况)
域名无ssl加密
WS.php写服务端代码
<?php
class WS{
private $ws = null;
public function __construct(){
$this->ws = new Swoole\WebSocket\Server('0.0.0.0', 9502);
$this->ws->on('Open', [$this, "onOpen"]);
$this->ws->on('Message', [$this, "onMessage"]);
$this->ws->on('Close', [$this, "onClose"]);
$this->ws->start();
}
public function onOpen($ws, $request){
var_dump($request->fd, $request->get, $request->server);
$ws->push($request->fd, "欢迎客户端: {$request->fd}\n");
}
public function onMessage($ws, $frame){
echo "信息: {$frame->data}\n";
foreach($ws->connections as $fd){
if($fd == $frame->fd){
$ws->push($fd, "我: {$frame->data}");
}else{
$ws->push($fd, "对方: {$frame->data}");
}
}
}
public function onClose($ws, $fd){
echo "客服端:{$fd} 关闭\n";
}
}
new WS();
static/chat.html写客户端代码
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<title>简易聊天室</title>
</head>
<div id="welcome"></div>
<input type="text" id="input"/>
<input type="buton" onclick="send()" value="发送"/>
<div id="message"></div>
<body>
</body>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script>
var wsServer = 'ws://xiangyu.huangzhongxin.cn:9502';
var websocket = new WebSocket(wsServer);
websocket.onopen = function (res) {
$("#welcome").append("<h1>连接成功!欢迎</h1>");
};
websocket.onclose = function (res) {
$("#message").append("<h3>连接关闭</h3>");
};
websocket.onmessage = function (res) {
$("#message").append("<h3>" + res.data + "</h3>");
};
websocket.onerror = function (res) {
$("#message").append("<h3>" + res + "</h3>");
};
function send(){
websocket.send($('#input').val());
}
</script>
</html>
域名有ssl加密
WS.php写服务端代码
<?php
class WS{
private $ws = null;
public function __construct(){
$this->ws = new Swoole\WebSocket\Server('0.0.0.0', 9502, SWOOLE_PROCESS, SWOOLE_SOCK_TCP | SWOOLE_SSL);
$this->ws->set([
'ssl_cert_file' => '/www/server/panel/vhost/cert/xxxxx/fullchain.pem',
'ssl_key_file' => '/www/server/panel/vhost/cert/xxxxx/privkey.pem',
]);
$this->ws->on('Open', [$this, "onOpen"]);
$this->ws->on('Message', [$this, "onMessage"]);
$this->ws->on('Close', [$this, "onClose"]);
$this->ws->start();
}
public function onOpen($ws, $request){
var_dump($request->fd, $request->get, $request->server);
$ws->push($request->fd, "欢迎客户端: {$request->fd}\n");
}
public function onMessage($ws, $frame){
echo "信息: {$frame->data}\n";
foreach($ws->connections as $fd){
if($fd == $frame->fd){
$ws->push($fd, "我: {$frame->data}");
}else{
$ws->push($fd, "对方: {$frame->data}");
}
}
}
public function onClose($ws, $fd){
echo "客服端:{$fd} 关闭\n";
}
}
new WS();
static/chat.html写客户端代码
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<title>简易聊天室</title>
</head>
<div id="welcome"></div>
<input type="text" id="input"/>
<input type="buton" onclick="send()" value="发送"/>
<div id="message"></div>
<body>
</body>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script>
var wsServer = 'wss://xiangyu.huangzhongxin.cn:9502';
var websocket = new WebSocket(wsServer);
websocket.onopen = function (res) {
$("#welcome").append("<h1>连接成功!欢迎</h1>");
};
websocket.onclose = function (res) {
$("#message").append("<h3>连接关闭</h3>");
};
websocket.onmessage = function (res) {
$("#message").append("<h3>" + res.data + "</h3>");
};
websocket.onerror = function (res) {
$("#message").append("<h3>" + res + "</h3>");
};
function send(){
websocket.send($('#input').val());
}
</script>
</html>