Skip to content

Commit

Permalink
cmdsrv => cs 简写
Browse files Browse the repository at this point in the history
  • Loading branch information
eyasliu committed Jan 26, 2021
1 parent 2b96a5d commit c6deb08
Show file tree
Hide file tree
Showing 36 changed files with 206 additions and 206 deletions.
2 changes: 1 addition & 1 deletion context.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cmdsrv
package cs

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion examples/chat/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<head>
<meta charset="UTF-8">
<title>CmdSrv Chat Demo</title>
<title>cs Chat Demo</title>
<link rel="stylesheet" href="style.css">
</head>

Expand Down
28 changes: 14 additions & 14 deletions examples/chat/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"errors"
"net/http"

"github.com/eyasliu/cmdsrv"
"github.com/eyasliu/cmdsrv/xhttp"
"github.com/eyasliu/cmdsrv/xwebsocket"
"github.com/eyasliu/cs"
"github.com/eyasliu/cs/xhttp"
"github.com/eyasliu/cs/xwebsocket"
)

func assert(err error) {
Expand All @@ -25,39 +25,39 @@ func main() {

go http.ListenAndServe(":12000", nil)

srv := cmdsrv.New(httpAdapter, wsAdapter)
srv := cs.New(httpAdapter, wsAdapter)
srv.Use(srv.AccessLogger("CHAT"))
srv.Use(cmdsrv.Recover())
srv.Use(cs.Recover())

srv.Handle("register", func(c *cmdsrv.Context) {
srv.Handle("register", func(c *cs.Context) {
var body struct {
Name string `p:"name" v:"required#名称必填" json:"name"`
}
assert(c.Parse(&body))

c.Set("name", body.Name)

c.Push(&cmdsrv.Response{
c.Push(&cs.Response{
Cmd: "welcome",
Data: "welcome " + body.Name + " to my chat room",
})

c.Broadcast(&cmdsrv.Response{
c.Broadcast(&cs.Response{
Cmd: "user_online",
Data: body,
})
// c.OK()
})

user := srv.Group(func(c *cmdsrv.Context) {
user := srv.Group(func(c *cs.Context) {
if c.Get("name") == nil {
c.Abort()
c.Err(errors.New("you are not login"), 101)
return
}
c.Next()
})
user.Handle("new_message", func(c *cmdsrv.Context) {
user.Handle("new_message", func(c *cs.Context) {
var body struct {
Message string `v:"required#消息不能为空"`
}
Expand All @@ -69,24 +69,24 @@ func main() {
"name": name,
"message": body.Message,
}
pushMsg := &cmdsrv.Response{
pushMsg := &cs.Response{
Cmd: "push_message",
Data: msg,
}
for _, sid := range c.GetAllSID() {
if c.GetState(sid, "name") != nil {
c.PushSID(sid, pushMsg)
} else {
c.PushSID(sid, &cmdsrv.Response{
c.PushSID(sid, &cs.Response{
Cmd: "hide_message",
})
}
}
})
user.Handle(cmdsrv.CmdClosed, func(c *cmdsrv.Context) {
user.Handle(cs.CmdClosed, func(c *cs.Context) {
for _, sid := range c.GetAllSID() {
if c.GetState(sid, "name") != nil {
c.PushSID(sid, &cmdsrv.Response{
c.PushSID(sid, &cs.Response{
Cmd: "user_offline",
Data: map[string]interface{}{
"name": c.Get("name"),
Expand Down
10 changes: 5 additions & 5 deletions examples/echo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package main
import (
"net/http"

"github.com/eyasliu/cmdsrv"
"github.com/eyasliu/cmdsrv/xhttp"
"github.com/eyasliu/cmdsrv/xwebsocket"
"github.com/eyasliu/cs"
"github.com/eyasliu/cs/xhttp"
"github.com/eyasliu/cs/xwebsocket"
)

func main() {
Expand All @@ -19,10 +19,10 @@ func main() {

go http.ListenAndServe(":13000", nil)

srv := cmdsrv.New(httpAdapter, wsAdapter)
srv := cs.New(httpAdapter, wsAdapter)
srv.Use(srv.AccessLogger("ECHOSRV"))

srv.Use(func(c *cmdsrv.Context) {
srv.Use(func(c *cs.Context) {
c.OK(c.RawData)
c.Abort()
})
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/eyasliu/cmdsrv
module github.com/eyasliu/cs

go 1.12

Expand Down
2 changes: 1 addition & 1 deletion heartbeat.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cmdsrv
package cs

import (
"sync"
Expand Down
2 changes: 1 addition & 1 deletion logger.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cmdsrv
package cs

import (
"encoding/json"
Expand Down
2 changes: 1 addition & 1 deletion middleware.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cmdsrv
package cs

func fillPushResp(c *Context) error {
c.Response.fill()
Expand Down
52 changes: 26 additions & 26 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Cmd Srv

[![Build Status](https://travis-ci.com/eyasliu/cmdsrv.svg)](https://travis-ci.com/eyasliu/cmdsrv)
[![Go Doc](https://godoc.org/github.com/eyasliu/cmdsrv?status.svg)](https://godoc.org/github.com/eyasliu/cmdsrv)
[![Code Coverage](https://codecov.io/gh/eyasliu/cmdsrv/branch/master/graph/badge.svg)](https://codecov.io/gh/eyasliu/cmdsrv/branch/master)
[![License](https://img.shields.io/github/license/eyasliu/cmdsrv.svg?style=flat)](https://github.com/eyasliu/cmdsrv)
[![Build Status](https://travis-ci.com/eyasliu/cs.svg)](https://travis-ci.com/eyasliu/cs)
[![Go Doc](https://godoc.org/github.com/eyasliu/cs?status.svg)](https://godoc.org/github.com/eyasliu/cs)
[![Code Coverage](https://codecov.io/gh/eyasliu/cs/branch/master/graph/badge.svg)](https://codecov.io/gh/eyasliu/cs/branch/master)
[![License](https://img.shields.io/github/license/eyasliu/cs.svg?style=flat)](https://github.com/eyasliu/cs)

开箱即用的基于命令的消息处理框架,让 websocket 和 tcp 开发就像 http 那样简单

Expand All @@ -14,8 +14,8 @@
package main
import (
"net/http"
"github.com/eyasliu/cmdsrv"
"github.com/eyasliu/cmdsrv/xwebsocket"
"github.com/eyasliu/cs"
"github.com/eyasliu/cs/xwebsocket"
)

func main() {
Expand All @@ -24,10 +24,10 @@ func main() {
http.Handle("/ws", ws)

srv := ws.Srv()
srv.Use(cmdsrv.AccessLogger("MYSRV")). // 打印请求响应日志
Use(cmdsrv.Recover()) // 统一错误处理,消化 panic 错误
srv.Use(cs.AccessLogger("MYSRV")). // 打印请求响应日志
Use(cs.Recover()) // 统一错误处理,消化 panic 错误

srv.Handle("register", func(c *cmdsrv.Context) {
srv.Handle("register", func(c *cs.Context) {
// 定义请求数据
var body struct {
UID int `p:"uid" v:"required"`
Expand All @@ -48,21 +48,21 @@ func main() {
})

// 给所有连接广播消息
c.Broadcast(&cmdsrv.Response{
c.Broadcast(&cs.Response{
Cmd: "someone_online",
Data: body,
})

// 往当前连接主动推送消息
c.Push(&cmdsrv.Response{
c.Push(&cs.Response{
Cmd: "welcome",
Data: "welcome to register my server",
})

// 遍历所有在线会话,获取其他会话的状态,并往指定会话推送消息
for _, sid := range c.GetAllSID() {
if c.Srv.GetState(sid, "uid") != nil {
c.Srv.Push(sid, &cmdsrv.Response{
c.Srv.Push(sid, &cs.Response{
Cmd: "firend_online",
Data: "your firend is online",
})
Expand All @@ -71,7 +71,7 @@ func main() {
})

// 分组
group := srv.Group(func(c *cmdsrv.Context) {
group := srv.Group(func(c *cs.Context) {
// 过滤指定请求
if _, ok := c.Get("uid").(int); !ok {
c.Err(errors.New("unregister session"), 101)
Expand All @@ -80,7 +80,7 @@ func main() {
c.Next()
})

group.Handle("userinfo", func(c *cmdsrv.Context) {
group.Handle("userinfo", func(c *cs.Context) {
uid := c.Get("uid").(int) // 中间件已处理过,可大胆断言
c.OK(map[string]interface{}{
"uid": uid,
Expand All @@ -100,7 +100,7 @@ func main() {
```go
import (
"net/http"
"github.com/eyasliu/cmdsrv/xwebsocket"
"github.com/eyasliu/cs/xwebsocket"
)

func main() {
Expand All @@ -117,7 +117,7 @@ func main() {

```go
import (
"github.com/eyasliu/cmdsrv/xtcp"
"github.com/eyasliu/cs/xtcp"
)

func main() {
Expand All @@ -135,7 +135,7 @@ func main() {

```go
import (
"github.com/eyasliu/cmdsrv/xgnet"
"github.com/eyasliu/cs/xgnet"
)

func main() {
Expand All @@ -154,8 +154,8 @@ func main() {
```go
import (
"net/http"
"github.com/eyasliu/cmdsrv"
"github.com/eyasliu/cmdsrv/xhttp"
"github.com/eyasliu/cs"
"github.com/eyasliu/cs/xhttp"
)

func main() {
Expand All @@ -174,10 +174,10 @@ func main() {
```go
import (
"net/http"
"github.com/eyasliu/cmdsrv"
"github.com/eyasliu/cmdsrv/xhttp"
"github.com/eyasliu/cmdsrv/xwebsocket"
"github.com/eyasliu/cmdsrv/xtcp"
"github.com/eyasliu/cs"
"github.com/eyasliu/cs/xhttp"
"github.com/eyasliu/cs/xwebsocket"
"github.com/eyasliu/cs/xtcp"
)

func main() {
Expand All @@ -197,7 +197,7 @@ func main() {
go tcp.Run()
go http.ListenAndServe(":8080", nil)

srv := cmdsrv.New(server, ws, tcp)
srv := cs.New(server, ws, tcp)
srv.Run() // 阻塞运行
}
```
Expand All @@ -222,8 +222,8 @@ $ curl -XPOST -H"Content-Type:application/json" --data '{"cmd":"register", "data

实现方案:

在 websocket 和 tcp 中,每个连接都抽象成一个字符串 `SID`, 即 Session ID, cmdsrv 只负责处理消息,不处理连接的任何状态,与连接和状态相关的操作全都以 interface 定义好,给各种工具去实现
在 websocket 和 tcp 中,每个连接都抽象成一个字符串 `SID`, 即 Session ID, cs 只负责处理消息,不处理连接的任何状态,与连接和状态相关的操作全都以 interface 定义好,给各种工具去实现

## API

[GoDoc](https://pkg.go.dev/github.com/eyasliu/cmdsrv)
[GoDoc](https://pkg.go.dev/github.com/eyasliu/cs)
2 changes: 1 addition & 1 deletion recover.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cmdsrv
package cs

import (
"runtime/debug"
Expand Down
2 changes: 1 addition & 1 deletion srv.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cmdsrv
package cs

import (
"errors"
Expand Down
2 changes: 1 addition & 1 deletion srv_group.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cmdsrv
package cs

// SrvGroup 路由组,用于实现分组路由
type SrvGroup struct {
Expand Down
18 changes: 9 additions & 9 deletions srv_norace_test.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
// +build !race test

package cmdsrv_test
package cs_test

import (
"github.com/eyasliu/cmdsrv"
"github.com/eyasliu/cs"
"github.com/gogf/gf/test/gtest"
"testing"
"time"
)

func TestSrv_MutilServer(t *testing.T) {
server1 := &testAdapter{request: []*cmdsrv.Request{{"a", "1", []byte{1}}}, sid: "1"}
server2 := &testAdapter{request: []*cmdsrv.Request{{"a", "2", []byte{2}}}, sid: "2"}
server3 := &testAdapter{request: []*cmdsrv.Request{{"a", "3", []byte{3}}}, sid: "3"}
server4 := &testAdapter{request: []*cmdsrv.Request{{"a", "4", []byte{4}}}, sid: "4"}
server5 := &testAdapter{request: []*cmdsrv.Request{{"a", "5", []byte{5}}}, sid: "5"}
server1 := &testAdapter{request: []*cs.Request{{"a", "1", []byte{1}}}, sid: "1"}
server2 := &testAdapter{request: []*cs.Request{{"a", "2", []byte{2}}}, sid: "2"}
server3 := &testAdapter{request: []*cs.Request{{"a", "3", []byte{3}}}, sid: "3"}
server4 := &testAdapter{request: []*cs.Request{{"a", "4", []byte{4}}}, sid: "4"}
server5 := &testAdapter{request: []*cs.Request{{"a", "5", []byte{5}}}, sid: "5"}
gtest.C(t, func(t *gtest.T) {
srv := cmdsrv.New(server1, server2)
srv := cs.New(server1, server2)
seqnos := []string{} // 这个变量会产生数据竞争,数据竞争就会导致里面数组的顺序不确定,但这只是测试代码
srv.Handle("a", func(c *cmdsrv.Context) {
srv.Handle("a", func(c *cs.Context) {
seqnos = append(seqnos, c.Seqno)
c.OK()
})
Expand Down
Loading

0 comments on commit c6deb08

Please sign in to comment.