Skip to content

Commit

Permalink
[add] WebSocket support
Browse files Browse the repository at this point in the history
  • Loading branch information
trheyi committed Mar 4, 2022
1 parent 80965c7 commit deb6ba9
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 2 deletions.
11 changes: 11 additions & 0 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@ func LoadFrom(dir string, prefix string) error {
log.With(log.F{"root": root, "file": filename}).Error(err.Error())
}
})

// Load WebSocket Server
err = share.Walk(dir, ".ws.json", func(root, filename string) {
name := prefix + share.SpecName(root, filename)
content := share.ReadFile(filename)
_, err := gou.LoadWebSocket(string(content), name)
if err != nil {
log.With(log.F{"root": root, "file": filename}).Error(err.Error())
}
})

return err
}

Expand Down
8 changes: 8 additions & 0 deletions api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/stretchr/testify/assert"
"github.com/yaoapp/gou"
"github.com/yaoapp/gou/websocket"
"github.com/yaoapp/yao/config"
)

Expand All @@ -20,5 +21,12 @@ func check(t *testing.T) {
for key := range gou.APIs {
keys = append(keys, key)
}

wskeys := []string{}
for key := range websocket.Upgraders {
wskeys = append(wskeys, key)
}

assert.Equal(t, 4, len(keys))
assert.Equal(t, 1, len(wskeys))
}
8 changes: 8 additions & 0 deletions cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/fatih/color"
"github.com/spf13/cobra"
"github.com/yaoapp/gou"
"github.com/yaoapp/gou/websocket"
"github.com/yaoapp/yao/config"
"github.com/yaoapp/yao/engine"
"github.com/yaoapp/yao/service"
Expand Down Expand Up @@ -63,7 +64,14 @@ var startCmd = &cobra.Command{
color.WhiteString(filepath.Join("/api", api.HTTP.Group, p.Path)),
"\tprocess:", p.Process)
}
}

fmt.Printf(color.CyanString("\n%s(%d)\n", "WebSocket", len(websocket.Upgraders)))
for name, upgrader := range websocket.Upgraders { // WebSocket
fmt.Println(
colorMehtod("GET"),
color.WhiteString(filepath.Join("/websocket", name)),
"\tprocess:", upgrader.Process)
}
}

Expand Down
2 changes: 1 addition & 1 deletion flow/flow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ func check(t *testing.T) {
for key := range gou.Flows {
keys = append(keys, key)
}
assert.Equal(t, 23, len(keys))
assert.Equal(t, 24, len(keys))
}
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,8 @@ github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
github.com/hashicorp/consul/api v1.1.0/go.mod h1:VmuI/Lkw1nC05EYQWNKwWGbkg+FbDBtguAZLlVdkD9Q=
github.com/hashicorp/consul/sdk v0.1.1/go.mod h1:VKf9jXwCTEY1QZP2MOLRhb5i/I/ssyNV1vwHyQBF0x8=
Expand Down
3 changes: 2 additions & 1 deletion service/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ func BinStatic(c *gin.Context) {

length := len(c.Request.URL.Path)

if length >= 5 && c.Request.URL.Path[0:5] == "/api/" { // API接口
if (length >= 5 && c.Request.URL.Path[0:5] == "/api/") ||
(length >= 11 && c.Request.URL.Path[0:11] == "/websocket/") { // API & websocket
c.Next()
return
} else if length >= 7 && c.Request.URL.Path[0:7] == "/xiang/" { // 数据管理后台
Expand Down
11 changes: 11 additions & 0 deletions tests/apis/chat.ws.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "A Chat WebSocket server",
"description": "A Chat WebSocket serverr",
"version": "0.9.2",
"protocols": ["yao-chat-01"],
"guard": "bearer-jwt",
"buffer": { "read": 1024, "write": 1024 },
"limit": { "read-wait": 10, "pong-wait": 20, "max-message": 1024 },
"timeout": 5,
"process": "flows.websocket.chat"
}
7 changes: 7 additions & 0 deletions tests/flows/websocket/chat.flow.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"label": "a chat Websocket process",
"version": "1.0.0",
"description": "a chat Websocket process",
"nodes": [],
"output": "{{$in.0}}"
}
5 changes: 5 additions & 0 deletions tests/scripts/websocket.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function Hello() {
var ws = new WebSocket("ws://127.0.0.1:5093/websocket/chat", "p0");
var response = ws.push("Hello World");
return response;
}

0 comments on commit deb6ba9

Please sign in to comment.