Skip to content
This repository was archived by the owner on May 18, 2020. It is now read-only.

Commit 37a8be4

Browse files
committed
websocket的demo
1 parent 04198c5 commit 37a8be4

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed

HttpServer.php

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
/**
3+
* @authors ShenYan ([email protected])
4+
* @date 2018-07-09 09:39:53
5+
* @boke https://qq52o.me
6+
*/
7+
# 创建http服务
8+
$http = new swoole_http_server("0.0.0.0", 9501);
9+
# 监听服务器请求
10+
$http->on('request', function ($request, $response) {
11+
$response->end("<h1>Hello Swoole. #" . rand(1000, 9999) . "</h1>");
12+
});
13+
# 启动服务
14+
$http->start();

WebSocket.php

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
/**
3+
* @authors ShenYan ([email protected])
4+
* @date 2018-07-09 09:52:20
5+
* @boke https://qq52o.me
6+
*/
7+
# 定义clientFds数组 保存所有websocket连接
8+
$clientFds = [];
9+
10+
# 创建websocket服务
11+
$server = new swoole_websocket_server("0.0.0.0", 9501);
12+
# 握手成功 触发回调函数
13+
$server->on('open', function (swoole_websocket_server $server, $request) use (&$clientFds) {
14+
# echo "server: handshake success with fd{$request->fd}\n";
15+
# 将所有客户端连接标识,握手成功后保存到数组中
16+
$clientFds[] = $request->fd;
17+
});
18+
# 收到消息 触发回调函数
19+
$server->on('message', function (swoole_websocket_server $server, $frame) use (&$clientFds) {
20+
# echo "receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish}\n";
21+
# $server->push($frame->fd, "this is server");
22+
# 当有用户发送信息,发送广播通知所有用户
23+
foreach ($clientFds as $fd) {
24+
$server->push($fd, $frame->data);
25+
}
26+
});
27+
# 关闭连接 触发回调函数
28+
$server->on('close', function ($ser, $fd) use (&$clientFds) {
29+
# echo "client {$fd} closed\n";
30+
# 关闭会话 销毁标识fd
31+
unset($clientFds[$fd]);
32+
});
33+
# 启动websocket服务
34+
$server->start();

chat.html

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>WebSocket聊天室</title>
6+
</head>
7+
<body>
8+
<div id="main" style="width:600px;height: 200px; overflow: auto;border: solid 2px black;">
9+
10+
</div>
11+
<textarea id="textarea"></textarea>
12+
<br/>
13+
<input type="button" value="发送数据" onclick="send()">
14+
<script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
15+
<script type="text/javascript">
16+
var name =prompt("请输入您的昵称","匿名者"); //弹出input框
17+
// 打开一个 web socket
18+
var ws = new WebSocket("ws://118.25.224.221:9501");
19+
20+
ws.onopen = function() {
21+
console.log("连接成功");
22+
};
23+
24+
//收到消息 触发回调
25+
ws.onmessage = function (evt) {
26+
var data = evt.data;
27+
console.log("收到socket服务消息,内容:" + data);
28+
$('#main').append("<p>" + data + "</p>");
29+
};
30+
31+
function send() {
32+
var data = document.getElementById('textarea').value;
33+
ws.send(name+ ":"+ data);
34+
}
35+
36+
ws.onclose = function() {
37+
// 关闭 websocket
38+
console.log("连接已关闭...");
39+
};
40+
</script>
41+
</body>
42+
</html>

0 commit comments

Comments
 (0)