Swoole学习(五)Swoole之简单WebSocket服务器的创建
环境:Centos6.4,PHP环境:PHP7
服务端代码
<?php
//创建websocket服务器
$host = '0.0.0.0';
$port = ;
$ws = new swoole_websocket_server($host, $port); //
$ws->on('open', function($ws, $request){ //$ws就是我们的服务器,$request就是客户端的信息
var_dump($request);
$ws->push($request->fd,"welcome \n");
}); $ws->on('message', function($ws, $request){
echo 'Message:'. $request->data;
$ws->push($request->fd,'get it message');
});
$ws->on('close', function($ws, $request){
echo 'close';
}); $ws->start();
客户端代码
<!DOCTYPE html>
<html>
<head>
<title>WebSecket</title>
</head>
<body>
<Script>
var wsServer = 'ws://192.168.9.155:9501';
var websocket = new WebSocket(wsServer);
websocket.onopen = function (evt) {
console.log("链接成功");
}; websocket.onclose = function (evt) {
console.log("关闭链接");
}; websocket.onmessage = function (evt) {
console.log('Retrieved data from server: ' + evt.data);
}; websocket.onerror = function (evt, e) {
console.log('Error occured: ' + evt.data);
};
</Script>
</body>
</html>
文件分别是index4.php、index4.html,。

出现此状态,没有报错,说明开启成功了。
# ps -ajft //查看进程状态

开始测试
浏览器访问index4.html

closeobject(Swoole\Http\Request)# () {
["fd"]=>
int()
["header"]=>
array() {
["host"]=>
string() "192.168.9.155:9501"
["connection"]=>
string() "Upgrade"
["pragma"]=>
string() "no-cache"
["cache-control"]=>
string() "no-cache"
["upgrade"]=>
string() "websocket"
["origin"]=>
string() "http://myec.com"
["sec-websocket-version"]=>
string() ""
["user-agent"]=>
string() "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.104 Safari/537.36 Core/1.53.4882.400 QQBrowser/9.7.13059.400"
["accept-encoding"]=>
string() "gzip, deflate, sdch"
["accept-language"]=>
string() "zh-CN,zh;q=0.8"
["sec-websocket-key"]=>
string() "/nsMgwOyqsM5xmgdqpEBRA=="
["sec-websocket-extensions"]=>
string() "permessage-deflate; client_max_window_bits"
}
["server"]=>
array() {
["request_method"]=>
string() "GET"
["request_uri"]=>
string() "/"
["path_info"]=>
string() "/"
["request_time"]=>
int()
["request_time_float"]=>
float(1522731778.6278)
["server_port"]=>
int()
["remote_port"]=>
int()
["remote_addr"]=>
string() "192.168.9.1"
["master_time"]=>
int()
["server_protocol"]=>
string() "HTTP/1.1"
["server_software"]=>
string() "swoole-http-server"
}
["request"]=>
NULL
["cookie"]=>
NULL
["get"]=>
NULL
["files"]=>
NULL
["post"]=>
NULL
["tmpfiles"]=>
NULL
}
OK~
Swoole学习(五)Swoole之简单WebSocket服务器的创建的更多相关文章
- Swoole学习(四)Swoole之简单WEB服务器的创建
环境:Centos6.4,PHP环境:PHP7 <?php //创建WEB服务器 $host = '0.0.0.0'; $port = ; $server = new swoole_http_s ...
- Swoole学习(七)Swoole之异步TCP服务器的创建
环境:Centos6.4,PHP环境:PHP7 <?php //创建TCP服务器 /** * $host 是swoole需要监听的ip,如果要监听本地,不对外服务,那么就是127.0.0.1;如 ...
- Swoole学习(三)Swoole之UDP服务器的创建
环境:Centos6.4,PHP环境:PHP7 <?php //创建UCP服务器(UDP服务器相对于TCP服务器通信更可靠些) /** * $host 是swoole需要监听的ip,如果要监听本 ...
- Swoole学习(二)Swoole之TCP服务器的创建
环境:Centos6.4,PHP环境:PHP7 <?php //创建TCP服务器 /** * $host 是swoole需要监听的ip,如果要监听本地,不对外服务,那么就是127.0.0.1;如 ...
- Netty实现简单WebSocket服务器
本文参考<Netty权威指南>├── WebSocketServerHandler.java├── WebSocketServer.java└── wsclient.html packag ...
- Swoole学习(六)Swoole之定时器的创建与清除
环境:Centos6.4,PHP环境:PHP7,Swoole2.1(在指定的时间后执行函数,需要1.7.7或更高版本) <?php //----------------------------- ...
- 04.swoole学习笔记--webSocket服务器
<?php //创建webSocket服务器 $serv=); //获取请求 //on //open 建立连接 $serv:服务器 $request:客户端信息 $serv->on('op ...
- swoole创建websocket服务器
目录 1 安装准备 1.1 安装swoole前必须保证系统已经安装了下列软件 1.2 下载并解压 1.3 编译安装成功后,修改php.ini 2 构建Swoole基本实例 2.1 tcp服务器实例 2 ...
- 01.swoole学习笔记--TCP服务器
1.安装swoole扩展 2.网络调试助手进行调试 <?php //创建服务器 $host='192.168.10.31'; $port=; //$model='SWOOLE_PROCESS'; ...
随机推荐
- iPhone 6 Screens Demystified
http://www.paintcodeapp.com/news/iphone-6-screens-demystified
- HDU 5306 Gorgeous Sequence[线段树区间最值操作]
Gorgeous Sequence Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...
- PHP 开发环境的搭建和使用03-- 安装mySql
1/ 安装的MySQL版本是5.6.10版本的,直接点击Install 2/ 选择 Execute 3/ 更新最新版本成功后,选择 "next" 4/ 自定义安装方式,选择C ...
- style,currentStyle和getComputedStyle的区别
样式表有三种方式 内嵌样式(inline Style) :是写在Tag里面的,内嵌样式只对所有的Tag有效. 内部样式(internal Style Sheet):是写在HTML的里面的,内部样式只对 ...
- FZU 2140 Forever 0.5(找规律,几何)
Problem 2140 Forever 0.5 Accept: 371 Submit: 1307 Special Judge Time Limit: 1000 mSec Memory Limit : ...
- R语言中的MySQL操作
R语言中,针对MySQL数据库的操作执行其实也有很多中方式.本人觉得,熟练掌握一种便可,下面主要就个人的学习使用情况,总结其中一种情况-----使用RMySQL操作数据库. 1.下载DBI和RMySQ ...
- 巨蟒python全栈开发flask12项目开始4
1.App实现录音功能 2.App上传录音 3.Toy消息提醒+消息存储 4.Toy收取消息 5.Chat页面完善&&本地播放&&网络播放&&获取聊天记 ...
- Microsoft Excel 标题栏或首行锁定
Microsoft Excel 标题栏或首行锁定 在进行Excel编辑的时候,希望在浏览的时候,第一行或者第一列能够始终显示. 需要做的是:在Excel中选择 "视图"->& ...
- jquery页面滚动显示浮动菜单栏锚点定位效果
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- 多进程使用matplotlib.pyplot绘heatmap(多线程不可以)
数据格式如下: 8_15/l_eye/2732.png -20.5773 -5.17769 -3.34583 21.5859 9_13_1/l_eye/1211.png -10.1145 34.992 ...