From da18ab1994e55de519c6597b15d3780c10f95c99 Mon Sep 17 00:00:00 2001 From: xiaodo <97404667@qq.com> Date: Mon, 14 Oct 2019 16:34:56 +0800 Subject: [PATCH] up websocket docs --- README.md | 48 ++++++++++++++++++--- cmd/application/static/ws.html | 76 --------------------------------- cmd/websocket.md | 32 +++++++++++--- resource/websocket.png | Bin 0 -> 900847 bytes 4 files changed, 69 insertions(+), 87 deletions(-) delete mode 100644 cmd/application/static/ws.html create mode 100644 resource/websocket.png diff --git a/README.md b/README.md index a5284bf..9f781ba 100644 --- a/README.md +++ b/README.md @@ -13,24 +13,62 @@ 相关md文档在cmd目录下,以及相关协议的demo测试 ``` `./cmd/*.md` -## @application 应用层 +## 一、相关协议进度 +### @application 应用层 - [x] http [docs](./cmd/http.md) - [x] websocket [docs](./cmd/websocket.md) -## @transport 传输层 +### @transport 传输层 - [x] tcp [docs](./cmd/tcp.md) - [x] udp [docs](./cmd/udp.md) - [x] port 端口机制 -## @network 网络层 +### @network 网络层 - [x] icmp - [x] ipv4 - [x] ipv6 -## @link 链路层 +### @link 链路层 - [x] arp [docs](./cmd/arp.md) - [x] ethernet -## @物理层 +### @物理层 - [x] tun tap 虚拟网卡的实现 +## 二、协议相关api +### 1.应用层相关协议 +> 应用层暂时只实现了http、websocket、等文本协议。都基于tcp实现,对tcp等进行二次封装 + +[`http`](./cmd/http.md): +``` +//新建一个初始化server,(底层 会创建一个tap网卡并注册 路由,arp缓存等),初始化端口机制,添加9502到端口表 +serv := http.NewHTTP("tap1", "192.168.1.0/24", "192.168.1.1", "9502") +//添加路由,当对应请求到来时,分发到自定义回调函数中处理 +serv.HandleFunc("/", func(request *http.Request, response *http.Response) +//赋值给将要发送响应给客户端的 buf +Response.End("string"); +//启动监听网卡、启动tcp、启动dispatch 分发事件并阻塞 等待client连接。。 +serv.ListenAndServ() +``` +[`websocket`](./cmd/websocket):初始化阶段和`httpserver`一致、新建httpserver、设置路由、启动监听服务 +``` +// r *http.Request, w *http.Response +// Upgrade 为校验websocket协议, 并将http协议升级为websocket协议。并接管http协议流程,直接进行tcp通讯保持连接, +c, err := websocket.Upgrade(r, w) + + //循环处理数据,接受数据,然后返回 + for { + //读取客户端数据,该方法一直阻塞直到收到客户端数据,会触发通道取消阻塞 + message, err := c.ReadData() + //发送数据给客户端,封装包头包体,调用tcpWrite 封装tcp包头,写入网络层 封装ip包头、写入链路层 封装以太网包头、写入网卡 + c.SendData([]byte("hello")) + } +``` + +### 2.传输层相关协议 +tcp + +udp + +port端口机制 + diff --git a/cmd/application/static/ws.html b/cmd/application/static/ws.html deleted file mode 100644 index 5e2839c..0000000 --- a/cmd/application/static/ws.html +++ /dev/null @@ -1,76 +0,0 @@ - -
- - - - -
- -点击 "Open" 开始一个新的WebSocket链接, -“Send" 将内容发送到服务器, -"Close" 将关闭链接。 - - - | - - |