forked from joewalnes/websocketd
-
Notifications
You must be signed in to change notification settings - Fork 1
/
websocket_endpoint.go
56 lines (47 loc) · 1.11 KB
/
websocket_endpoint.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
// Copyright 2013 Joe Walnes and the websocketd team.
// All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package libwebsocketd
import (
"io"
"code.google.com/p/go.net/websocket"
)
type WebSocketEndpoint struct {
ws *websocket.Conn
output chan string
log *LogScope
}
func NewWebSocketEndpoint(ws *websocket.Conn, log *LogScope) *WebSocketEndpoint {
return &WebSocketEndpoint{
ws: ws,
output: make(chan string),
log: log}
}
func (we *WebSocketEndpoint) Terminate() {
}
func (we *WebSocketEndpoint) Output() chan string {
return we.output
}
func (we *WebSocketEndpoint) Send(msg string) bool {
err := websocket.Message.Send(we.ws, msg)
if err != nil {
we.log.Trace("websocket", "Cannot send: %s", err)
return false
}
return true
}
func (we *WebSocketEndpoint) ReadOutput(config *Config) {
for {
var msg string
err := websocket.Message.Receive(we.ws, &msg)
if err != nil {
if err != io.EOF {
we.log.Debug("websocket", "Cannot receive: %s", err)
}
break
}
we.output <- msg
}
close(we.output)
}