Skip to content

Commit

Permalink
update README
Browse files Browse the repository at this point in the history
  • Loading branch information
Allenxuxu committed Dec 25, 2019
1 parent aba3698 commit 26b53e5
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 1 deletion.
62 changes: 62 additions & 0 deletions README-ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
- 支持多核多线程
- 动态扩容 Ring Buffer 实现的读写缓冲区
- 异步读写
- 自动清理空闲连接
- SO_REUSEPORT 端口重用支持
- 支持 WebSocket/Protobuf
- 支持定时任务,延时任务
Expand Down Expand Up @@ -211,6 +212,9 @@ func (c *Connection) ShutdownWrite() error

WebSocket 协议构建在 TCP 协议之上的,所以 `gev` 无需内置它,而是通过插件的形式提供支持,在 `plugins/websocket` 目录。

<details>
<summary> code </summary>

```go
type Protocol struct {
upgrade *ws.Upgrader
Expand Down Expand Up @@ -260,6 +264,8 @@ func (p *Protocol) Packet(c *connection.Connection, data []byte) []byte {
}
```

</details>

详细实现可以插件实现查看 [源码](plugins/websocket),使用方式可以查看 websocket [示例](example/websocket)

## 示例
Expand Down Expand Up @@ -316,6 +322,62 @@ func main() {

</details>

<details>
<summary> 主动断开空闲连接 </summary>

```go
package main

import (
"flag"
"strconv"
"time"

"github.com/Allenxuxu/gev"
"github.com/Allenxuxu/gev/connection"
"github.com/Allenxuxu/gev/log"
)

type example struct {
}

func (s *example) OnConnect(c *connection.Connection) {
log.Info(" OnConnect : ", c.PeerAddr())
}
func (s *example) OnMessage(c *connection.Connection, ctx interface{}, data []byte) (out []byte) {
log.Infof("OnMessage from : %s", c.PeerAddr())
out = data
return
}

func (s *example) OnClose(c *connection.Connection) {
log.Info("OnClose: ", c.PeerAddr())
}

func main() {
handler := new(example)
var port int
var loops int

flag.IntVar(&port, "port", 1833, "server port")
flag.IntVar(&loops, "loops", -1, "num loops")
flag.Parse()

s, err := gev.NewServer(handler,
gev.Network("tcp"),
gev.Address(":"+strconv.Itoa(port)),
gev.NumLoops(loops),
gev.IdleTime(5*time.Second))
if err != nil {
panic(err)
}

s.Start()
}
```

</details>

<details>
<summary> 限制最大连接数 </summary>

Expand Down
63 changes: 62 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Support custom protocols to quickly and easily build high-performance servers.
- Dynamic expansion of read and write buffers implemented by Ring Buffer
- Asynchronous read and write
- SO_REUSEPORT port reuse support
- Automatically clean up idle connections
- Support WebSocket/Protobuf
- Support for scheduled tasks, delayed tasks
- Support for custom protocols
Expand Down Expand Up @@ -212,6 +213,9 @@ func (c *Connection) ShutdownWrite() error

The WebSocket protocol is built on top of the TCP protocol, so gev doesn't need to be built in, but instead provides support in the form of plugins, in the plugins/websocket directory.

<details>
<summary> code </summary>

```go
type Protocol struct {
upgrade *ws.Upgrader
Expand Down Expand Up @@ -261,6 +265,8 @@ func (p *Protocol) Packet(c *connection.Connection, data []byte) []byte {
}
```

</details>

The detailed implementation can be viewed by the [plugin](plugins/websocket). The source code can be viewed using the [websocket example](example/websocket).

## Example
Expand Down Expand Up @@ -317,6 +323,62 @@ func main() {

</details>

<details>
<summary> Automatically clean up idle connections </summary>

```go
package main

import (
"flag"
"strconv"
"time"

"github.com/Allenxuxu/gev"
"github.com/Allenxuxu/gev/connection"
"github.com/Allenxuxu/gev/log"
)

type example struct {
}

func (s *example) OnConnect(c *connection.Connection) {
log.Info(" OnConnect : ", c.PeerAddr())
}
func (s *example) OnMessage(c *connection.Connection, ctx interface{}, data []byte) (out []byte) {
log.Infof("OnMessage from : %s", c.PeerAddr())
out = data
return
}

func (s *example) OnClose(c *connection.Connection) {
log.Info("OnClose: ", c.PeerAddr())
}

func main() {
handler := new(example)
var port int
var loops int

flag.IntVar(&port, "port", 1833, "server port")
flag.IntVar(&loops, "loops", -1, "num loops")
flag.Parse()

s, err := gev.NewServer(handler,
gev.Network("tcp"),
gev.Address(":"+strconv.Itoa(port)),
gev.NumLoops(loops),
gev.IdleTime(5*time.Second))
if err != nil {
panic(err)
}

s.Start()
}
```

</details>

<details>
<summary> Maximum connections </summary>

Expand Down Expand Up @@ -739,7 +801,6 @@ func main() {

</details>


## Thanks

Thanks JetBrains for the free open source license
Expand Down

0 comments on commit 26b53e5

Please sign in to comment.