-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
78 lines (66 loc) · 1.6 KB
/
server.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
72
73
74
75
76
77
78
package gometeor
import (
"errors"
"fmt"
"github.com/mrlauer/gosockjs"
"log"
"reflect"
"sync"
)
type Server struct {
uuid string
functions map[string]interface{}
functionLock sync.RWMutex
}
func (s *Server) sockjsHandler(c *gosockjs.Conn) {
// Acknowledge.
ack, err := ToJSON(struct{ Server_id string }{s.uuid})
if err != nil {
log.Print(err)
return
}
c.Write(ack)
// We should probably do this with callbacks.
// Create and start a Stream, a thin layer on top of sockjs.
// Also create a session.
// Hand control to the stream and session.
session := newSession(s, c)
session.Run()
}
func (s *Server) HandleHTTP(baseUrl string) {
// The protocol is built on SockJS, so install SockJS handlers.
gosockjs.Install(baseUrl, func(c *gosockjs.Conn) {
s.sockjsHandler(c)
})
}
func NewServer() *Server {
s := &Server{}
s.uuid = uuid()
s.functions = make(map[string]interface{})
return s
}
func (s *Server) Methods(methods map[string]interface{}) error {
s.functionLock.Lock()
defer s.functionLock.Unlock()
for name := range methods {
if _, ok := s.functions[name]; ok {
return fmt.Errorf("There is already a method named %s\n", name)
}
}
for name, function := range methods {
val := reflect.ValueOf(function)
if val.Kind() != reflect.Func {
return errors.New("Not a function.")
}
if val.Type().NumOut() > 1 {
return errors.New("Registered functions may return only one result.")
}
s.functions[name] = function
}
return nil
}
func (s *Server) GetFunction(name string) interface{} {
s.functionLock.RLock()
defer s.functionLock.RUnlock()
return s.functions[name]
}