forked from YaoApp/yao
-
Notifications
You must be signed in to change notification settings - Fork 0
/
websocket.go
71 lines (63 loc) · 1.72 KB
/
websocket.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package cmd
import (
"fmt"
"strings"
"github.com/fatih/color"
"github.com/spf13/cobra"
"github.com/yaoapp/gou"
"github.com/yaoapp/kun/exception"
"github.com/yaoapp/yao/config"
"github.com/yaoapp/yao/engine"
"github.com/yaoapp/yao/share"
)
var websocketCmd = &cobra.Command{
Use: "websocket",
Short: L("Open a websocket connection"),
Long: L("Open a websocket connection"),
Run: func(cmd *cobra.Command, args []string) {
defer share.SessionStop()
defer gou.KillPlugins()
defer func() {
err := exception.Catch(recover())
if err != nil {
fmt.Println(color.RedString(L("Fatal: %s"), err.Error()))
}
}()
Boot()
cfg := config.Conf
cfg.Session.IsCLI = true
engine.Load(cfg)
if len(args) < 1 {
fmt.Println(color.RedString(L("Not enough arguments")))
fmt.Println(color.WhiteString(share.BUILDNAME + " help"))
return
}
name := args[0]
websocket, has := gou.WebSockets[name]
if !has {
fmt.Println(color.RedString(L("%s not exists!"), name))
return
}
url := websocket.URL
protocols := websocket.Protocols
argsLen := len(args)
if argsLen > 1 {
url = args[1]
}
if argsLen > 2 {
protocols = args[2:]
}
fmt.Println(color.WhiteString("\n---------------------------------"))
fmt.Println(color.WhiteString(websocket.Name))
fmt.Println(color.WhiteString("---------------------------------"))
fmt.Println(color.GreenString(" URL: %s", url))
fmt.Println(color.GreenString("Protocols: %s", strings.Join(protocols, ",")))
fmt.Println(color.WhiteString("--------------------------------------"))
pargs := append([]string{url}, protocols...)
err := websocket.Open(pargs...)
if err != nil {
fmt.Println(color.RedString(L("%s"), err.Error()))
return
}
},
}