一、代码部分

<?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的更多相关文章

  1. swoole之建立 http server

    一.代码部分 <?php /** * 传统:nginx <-> php-fpm(fast-cgi process manager) <-> php * swoole:ht ...

  2. swoole之建立 tcp server

    一.swoole的安装 参照官网:https://wiki.swoole.com/wiki/page/6.html 二.代码部分 服务端: <?php $host = "127.0.0 ...

  3. 利用Swoole实现PHP+websocket直播,即使通讯

    websocket Websocket只是一个网络通信协议,就像 http.ftp等都是网络通信的协议一样:相对于HTTP这种非持久的协议来说,Websocket是一个持久化网络通信的协议: WebS ...

  4. Swoole 中使用 WebSocket 异步服务器、WebSocket 协程服务器

    WebSocket 异步风格服务器 WebSocket\Server 继承自 Http\Server,所以 Http\Server 提供的所有 API 和配置项都可以使用. # ws_server.p ...

  5. 使用Jetty搭建Java Websocket Server,实现图像传输

    https://my.oschina.net/yushulx/blog/298140 How to Implement a Java WebSocket Server for Image Transm ...

  6. 使用Identity Server 4建立Authorization Server (1)

    预备知识: http://www.cnblogs.com/cgzl/p/7746496.html 本文内容基本完全来自于Identity Server 4官方文档: https://identitys ...

  7. 从头编写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 基础框架: 第 ...

  8. SpringBoot报错:Failed to load ApplicationContext(javax.websocket.server.ServerContainer not available)

    引起条件: WebSocket+单元测试,单元测试报错! 解决方法: SpringBootTest增加webEnvironment参数. https://docs.spring.io/spring-b ...

  9. 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 ...

随机推荐

  1. pandas读取文件的read_csv()方法

    import pandas as pd pd.read_csv(filepath_or_buffer,header,parse_dates,index_col) 返回数据类型:DataFrame:二维 ...

  2. 【K-means算法】matlab代码实例学习

    1. MATLAB函数Kmeans 使用方法:Idx=Kmeans(X,K)[Idx,C]=Kmeans(X,K) [Idx,C,sumD]=Kmeans(X,K) [Idx,C,sumD,D]=Km ...

  3. 时间选择器UIDatePicker的使用

    UIDatePicker的介绍 UIDatePicker这个类的对象让用户可以在多个车轮上选择日期和时间.iPhone手机上的‘时钟’应用程序中的时间与闹铃中便使用了该控件.使用这个控件时,如果你能配 ...

  4. 微信小程序加密解密 C# 以及 填充无效,无法被移除错误的解决方案 Padding is invalid and cannot be removed

    解密加密源码 using System; using System.Security.Cryptography; using System.Text; namespace Wechat { publi ...

  5. scrapy extention实战-空闲时关闭爬虫

    scrapy extention实战 1.      空闲-关闭 使用扩展+spider_idle信号关闭爬虫. 启用扩展:settings.py EXTENSIONS = {     #'scrap ...

  6. ubuntu13.10安装增强功能

    步骤: 1>cd /mnt 2> ./VBoxLinuxAdditions.run 3>设置共享文件夹share 4>访问共享文件夹cd /media/sf_share not ...

  7. notepad++常用命令

    1>shift+tab减少缩进 2>tab键增加缩进 3>ctrol+d键复制多行 4>shift+alt+方向箭头,选中列 5>alt+c插入序列 6>ctrl+ ...

  8. Struts2学习(四)

    struts-defualt.xml指定的result的类型 1.struts-defualt.xml 文件的 181 行 开始定义了: <result-types> <result ...

  9. java中内部类的实例化

  10. PL-USB2-BLASTER 使用说明

    PL-USB2-BLASTER 使用说明 PL-USB2-BLASTER就是USB BLATER II烧录器.官方文档在https://www.intel.com/content/dam/www/pr ...