[add] WebSocket support
This commit is contained in:
parent
80965c78e3
commit
deb6ba9135
9 changed files with 55 additions and 2 deletions
11
api/api.go
11
api/api.go
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ import (
|
|||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/yaoapp/gou"
|
||||
"github.com/yaoapp/gou/websocket"
|
||||
"github.com/yaoapp/yao/config"
|
||||
)
|
||||
|
||||
|
|
@ -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))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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
go.sum
2
go.sum
|
|
@ -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=
|
||||
|
|
|
|||
|
|
@ -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/" { // 数据管理后台
|
||||
|
|
|
|||
11
tests/apis/chat.ws.json
Normal file
11
tests/apis/chat.ws.json
Normal file
|
|
@ -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
tests/flows/websocket/chat.flow.json
Normal file
7
tests/flows/websocket/chat.flow.json
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
{
|
||||
"label": "a chat Websocket process",
|
||||
"version": "1.0.0",
|
||||
"description": "a chat Websocket process",
|
||||
"nodes": [],
|
||||
"output": "{{$in.0}}"
|
||||
}
|
||||
5
tests/scripts/websocket.js
Normal file
5
tests/scripts/websocket.js
Normal file
|
|
@ -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;
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue