一、代码部分

<?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. Python中利用for表达式创建列表

    1.for表达式语法格式及用法 for表达式利用可迭代对象创建新的列表,for表达式也称为列表推导式,具体语法格式如下: [表达式 for 循环计数器 in 可迭代对象] 例: a = [ i + i ...

  2. TreeView 中 MVVM Command Binding

    <HierarchicalDataTemplate x:Key="TreeNodes" ItemsSource="{Binding Path=Childs,Mode ...

  3. 笔记-mongodb数据操作

    笔记-mongodb数据操作 1.      数据操作 1.1.    插入 db.COLLECTION_NAME.insert(document) 案例: db.inventory.insertOn ...

  4. 泛型DAO模型设计

    aaarticlea/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAMCAgMCAgMDAwMEAwMEBQgFBQQEBQoHBwYIDAoMDAsKC

  5. 汇编语言从入门到精通-5微机CPU的指令系统1

    微机CPU的指令系统 5.1 汇编语言指令格式 为了介绍指令系统中指令的功能,先要清楚汇编语言是如何书写指令的,这就象在学习高级语言程序设计时,要清楚高级语言语句的语义.语法及其相关规定一样. 5.1 ...

  6. Python学习笔记005

    if if     ==    : xxxx elif     : xxxx else: xxxx 输入字符串 input() 字符串转数值 int() 数值转字符串 str() 输出 print() ...

  7. in comment after two dashes (--) next character must be > not - (position: START_TAG seen ...

    Error executing Maven. in comment after two dashes (--) next character must be > not - (position: ...

  8. 个人相关API - 行驶方向判断函数

    calculateDrivingDirection(newCoordinates,oldCoordinates){ let o = { direction: '', deviation:null, i ...

  9. Vue源码(上篇)

    某课网有个488人名币的源码解读视频看不起,只能搜很多得资料慢慢理解,看源码能知道大佬的功能模块是怎么分块写的,怎么复用的,已经vue是怎么实现的 资料来自 vue源码 喜欢唱歌的小狮子 web喵喵喵 ...

  10. 配置VSCode开发Vue项目

    一.安装VSCode.NodeJS VSCode:https://code.visualstudio.com/ NodeJS:https://nodejs.org/en/ 二.打开VSCode,安装常 ...