-
Notifications
You must be signed in to change notification settings - Fork 0
/
ws.go
51 lines (42 loc) · 1.32 KB
/
ws.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
// Copyright (c) Jeevanandam M. (https://github.com/jeevatkm)
// aahframework.org/ws source code and usage is governed by a MIT style
// license that can be found in the LICENSE file.
// Package ws is a WebSocket library for aah framework (RFC 6455 compliant).
//
// aah ws internally it uses tiny, efficient WebSocket library
// (http://github.com/gobwas/ws) developed by Sergey Kamardin
// (https://github.com/gobwas).
package ws
import (
"fmt"
"net/url"
"aahframework.org/ainsp.v0"
)
// New method creates aah WebSocket engine with given aah application instance :)
func New(app interface{}) (*Engine, error) {
a, ok := app.(application)
if !ok {
return nil, fmt.Errorf("ws: not a valid aah application instance")
}
eng := &Engine{
app: a,
registry: &ainsp.TargetRegistry{
Registry: make(map[string]*ainsp.Target),
SearchType: ctxPtrType,
},
}
keyPrefix := "server.websocket"
eng.checkOrigin = a.Config().BoolDefault(keyPrefix+".origin.check", false)
// parse whitelist origin urls
eng.originWhitelist = make([]*url.URL, 0)
if originWhitelist, found := a.Config().StringList(keyPrefix + ".origin.whitelist"); found {
for _, o := range originWhitelist {
u, err := url.Parse(o)
if err != nil {
return nil, err
}
eng.originWhitelist = append(eng.originWhitelist, u)
}
}
return eng, nil
}