swoole之建立 websocket server
一、代码部分
<?php
/**
* 为什么用WebSocket?
* HTTP的通信只能由客户端发起
*
* WebSocket 协议是基于TCP的一种新的网络协议。实现了浏览器与服务器全双工通信——允许服务器主动发送信息给客户端。
*
* WebSocket 特点:
* 1、建立在TCP协议之上
* 2、性能开销下通信高效
* 3、客户端可以与任意服务器通信
* 4、协议标识符ws wss
* 5、持久化网络通信协议
*/ use Swoole\WebSocket\Server; $server = new Server("0.0.0.0", 9502); // 连接并完成握手后会回调此函数
$server->on('open', function (Swoole\WebSocket\Server $server, $request) {
echo "server: handshake success with fd {$request->fd}\n";
}); // 必选 监听ws消息事件
$server->on('message', function (Swoole\WebSocket\Server $server, $frame) {
echo "receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish}\n";
$server->push($frame->fd, "this is server");
}); $server->on('close', function ($ser, $fd) {
echo "client {$fd} closed\n";
}); $server->start();
客户端:
<!DOCTYPE html>
<html lang="en"> <head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>websocket client</title>
</head> <body>
<h1>swoole-ws测试</h1>
<script>
var wsUrl = 'ws://192.168.199.138:9502';
var websocket = new WebSocket(wsUrl); websocket.onopen = function (evt) {
console.log("connect success");
websocket.send('hello-swoole');
}; websocket.onmessage = function (evt) {
console.log("server return data: " + evt.data);
}; websocket.onclose = function (evt) {
console.log("connect close");
}; websocket.onerror = function (evt, e) {
console.log("error: " + evt.data);
}
</script>
</body> </html>
开启HTTP Server下,通过浏览器url访问
比如: http://192.168.199.138:8888/ws_client.html
swoole之建立 websocket server的更多相关文章
- swoole之建立 http server
一.代码部分 <?php /** * 传统:nginx <-> php-fpm(fast-cgi process manager) <-> php * swoole:ht ...
- swoole之建立 tcp server
一.swoole的安装 参照官网:https://wiki.swoole.com/wiki/page/6.html 二.代码部分 服务端: <?php $host = "127.0.0 ...
- 利用Swoole实现PHP+websocket直播,即使通讯
websocket Websocket只是一个网络通信协议,就像 http.ftp等都是网络通信的协议一样:相对于HTTP这种非持久的协议来说,Websocket是一个持久化网络通信的协议: WebS ...
- Swoole 中使用 WebSocket 异步服务器、WebSocket 协程服务器
WebSocket 异步风格服务器 WebSocket\Server 继承自 Http\Server,所以 Http\Server 提供的所有 API 和配置项都可以使用. # ws_server.p ...
- 使用Jetty搭建Java Websocket Server,实现图像传输
https://my.oschina.net/yushulx/blog/298140 How to Implement a Java WebSocket Server for Image Transm ...
- 使用Identity Server 4建立Authorization Server (1)
预备知识: http://www.cnblogs.com/cgzl/p/7746496.html 本文内容基本完全来自于Identity Server 4官方文档: https://identitys ...
- 从头编写asp.net core 2.0 web api 基础框架 (5) + 使用Identity Server 4建立Authorization Server (7) 可运行前后台源码
前台使用angular 5, 后台是asp.net core 2.0 web api + identity server 4. 从头编写asp.net core 2.0 web api 基础框架: 第 ...
- SpringBoot报错:Failed to load ApplicationContext(javax.websocket.server.ServerContainer not available)
引起条件: WebSocket+单元测试,单元测试报错! 解决方法: SpringBootTest增加webEnvironment参数. https://docs.spring.io/spring-b ...
- io.undertow.websockets.jsr.ServerWebSocketContainer cannot be cast to org.apache.tomcat.websocket.server.WsServerContainer
Caused by: java.lang.ClassCastException: io.undertow.websockets.jsr.ServerWebSocketContainer cannot ...
随机推荐
- hdu1698 区间更新
初写线段树的时候,印象最深的一道,有一个pushdown的操作,使我的tle变成了ac 题意 输入t,然后t组数据 输入n,m,n代表n个点上价值全是1的绳子,m代表m次操作 m行l,r,val 就 ...
- vue.js 第九课
这次讲红色框框. 方法与事件处理器: 方法处理器: 内联语句处理器: 事件修饰符: 按键修饰符: 为什么在HTML中监听事件? 1.v-on绑定事件 带参数 2.事件对象$event: 有时也需要调用 ...
- 动态代理Cglib
jar包 <!-- https://mvnrepository.com/artifact/cglib/cglib --><dependency> <groupId> ...
- vbs 入门
dim 定义变量名 dim name------------- dim name,agename = "worf"age = 20 msgbox 输出 msgbox name ...
- vim锁定,不能动
在vim中 ctrl+s是锁屏命令, ctrl+q是解锁
- 支持USB4的Linux 5.6,有望在今年4月份推出
导读 根据外媒Phoronix的报道,Linux 5.6将支持USB4,. USB4的规范在去年9月份发布,基于雷电3,并与之向后兼容.英特尔的开源部门在去年10月份添加了USB4的初始补丁. 据报道 ...
- HDU 5570:balls 期望。。。。。。。。。。。。。。。
balls Accepts: 19 Submissions: 55 Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/655 ...
- 在win10中启动redis
1.输入命令"redis-server.exe redis.windows.conf ",按回车键, 2.输入“redis-cli.exe -h 127.0.0.1 -p 6379 ...
- 浏览器控制台报Cannot read property 'conf' of undefined
原因:JS中有个变量没有类型导致 解决:加上类型即可(我是少写了var)
- Vue源码(下篇)
上一篇是mount之前的添加一些方法,包括全局方法gloal-api,XXXMixin,initXXX,然后一切准备就绪,来到了mount阶段,这个阶段主要是 解析template 创建watcher ...