一、代码部分

<?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. vue 和 jquery混合使用

    有时候只要想到要用的 vue.js 的时候就会惯性的想起用vue-cli手脚架搭建一个项目,但是有时候的业务场景并不适合用vue-cli手脚架,这个时候使用vue+jquery混合使用,把他们的优点结 ...

  2. c# 分页 PaginatedList<TResult>

    using System; using System.Collections.Generic; using System.Linq; namespace Microestc.PaginatedList ...

  3. 编写自己的JDBC框架

    目的 简化代码,提高开发效率 设计模式 策略设计模式 代码 #连接设置 driverClassName=com.mysql.jdbc.Driver url=jdbc:mysql://localhost ...

  4. nginx日志模块与HTTP过滤模块与sub模块修改返回内容

    日志格式使用指令 指令介绍 Syntax: log_format name [escape=default|json|none] string ...; Default: log_format com ...

  5. windows 以太坊开发框架Truffle环境搭建

    https://www.jianshu.com/p/f7a4de0cba9d 一.安装DApp开发环境 1.1 安装Node.js 我们使用官方长期支持的8.10.0LTS版本,下载64位包装包. 下 ...

  6. Spring学习(五)

    自动装备 1.定义 自动装配(autowiring): 将某个Bean实例中所引用的其它的Bean自动注入到当前Bean实例中 自动装配就是指由Spring来自动地注入依赖对象,无需人工参与. 自动装 ...

  7. System.arraycopy方法解释

    数组拷贝 public static native void arraycopy(Object src, int srcPos, Object dest, int destPos, int lengt ...

  8. springboot之快速创建项目

    1.选择创建新项目: 2.选择spring initializr,然后next 3.填写项目元数据,然后next 4.选择项目依赖,然后next 5.点击finish,完成项目创建

  9. Linux centosVMware 负载均衡集群介绍、LVS介绍、LVS调度算法、LVS NAT模式搭建

    一.负载均衡集群介绍 主流开源软件LVS.keepalived.haproxy.nginx等 其中LVS属于4层(网络OSI 7层模型),nginx属于7层,haproxy既可以认为是4层,也可以当做 ...

  10. Linux centos7VMware Apache和PHP结合、Apache默认虚拟主机

    一.Apache和PHP结合 httpd主配置文件/usr/local/apache2.4/conf/httpd.conf 启动报错 [root@davery ~]# /usr/local/apach ...