Skip to content

Commit

Permalink
up websocket docs
Browse files Browse the repository at this point in the history
  • Loading branch information
brewlin committed Oct 14, 2019
1 parent 0389800 commit da18ab1
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 87 deletions.
48 changes: 43 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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端口机制

76 changes: 0 additions & 76 deletions cmd/application/static/ws.html

This file was deleted.

32 changes: 26 additions & 6 deletions cmd/websocket.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,39 @@ sudo ./websocket
```
## @websocketserver.go
```
package main
import (
"fmt"
"log"
"github.com/brewlin/net-protocol/pkg/logging"
"github.com/brewlin/net-protocol/protocol/application/http"
"github.com/brewlin/net-protocol/protocol/application/websocket"
)
func init() {
logging.Setup()
}
func main() {
serv := http.NewHTTP("tap1", "192.168.1.0/24", "192.168.1.1", "9502")
serv.HandleFunc("/websocket", echo)
serv.HandleFunc("/ws", echo)
serv.HandleFunc("/", func(request *http.Request, response *http.Response) {
response.End("hello")
})
fmt.Println("@main: server is start ip:192.168.1.1 port:9502 ")
serv.ListenAndServ()
}
//websocket处理器
func echo(r *http.Request, w *http.Response) {
//协议升级
fmt.Println("got http request ; start to upgrade websocket protocol....")
//协议升级 c *websocket.Conn
c, err := websocket.Upgrade(r, w)
if err != nil {
log.Print("Upgrade error:", err)
//升级协议失败,直接return 交由http处理响应
fmt.Println("Upgrade error:", err)
return
}
defer c.Close()
Expand All @@ -38,9 +55,12 @@ func echo(r *http.Request, w *http.Response) {
log.Println("read:", err)
break
}
log.Printf("recv:%s", message)
c.SendData(message)
fmt.Println("recv client msg:", string(message))
// c.SendData(message )
c.SendData([]byte("hello"))
}
}
```
```
## demo
![](/resource/websocket.png)
Binary file added resource/websocket.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit da18ab1

Please sign in to comment.