/
wssocket.go
142 lines (110 loc) · 2.97 KB
/
wssocket.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package sckt
import (
"github.com/gorilla/websocket"
"net/http"
"log"
"strconv"
"net"
"fmt"
"errors"
)
type wsSocket struct {
id int
Port int
Uri string
onRx chan <- *Datagram
wscon *websocket.Conn
wsCons map[string]*websocket.Conn
localaddr net.Addr
}
func NewWSSocket(SocketID int, Uri string, Port int, chRx chan <-*Datagram) (Socket, error) {
sckt := new(wsSocket)
sckt.Port = Port
sckt.Uri = Uri
sckt.onRx = chRx
sckt.id = SocketID
sckt.wsCons = make(map[string]*websocket.Conn)
return sckt, nil
}
func (sckt *wsSocket) Write(data []byte, Addr net.Addr) (int, error) {
conn := sckt.wsCons[Addr.String()]
if (conn == nil) {
return 0, errors.New("No open WS-Connection for this Remote Address!")
}
fmt.Printf("Writing to ws-Socket on %s\r\n", Addr.String())
err := conn.WriteMessage(websocket.BinaryMessage, data)
n := len(data)
if (err != nil) {
n = 0
fmt.Println(err)
}
return n, err
}
func (sckt *wsSocket)Close() error {
return sckt.Close()
}
func (sckt *wsSocket)SocketID() int {
return sckt.id
}
func (sckt *wsSocket)ReceiveCh(newChan chan <-*Datagram) chan <-*Datagram {
if newChan != nil {
sckt.onRx = newChan
}
return sckt.onRx
}
func (sckt *wsSocket) reqHandler(w http.ResponseWriter, r *http.Request) {
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
CheckOrigin: func(r *http.Request) bool {
return true
},
}
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Println(err)
return
}
sckt.wsCons[conn.RemoteAddr().String()] = conn
defer conn.Close()
defer delete(sckt.wsCons, conn.RemoteAddr().String())
fmt.Printf("New WS-Connection to %s from %s\r\n", conn.LocalAddr().String(), conn.RemoteAddr().String())
fmt.Println("My Connections now:\r\n", sckt.wsCons)
for {
//messageType, binData, err := conn.ReadMessage()
messageType, binData, err := conn.ReadMessage()
if err != nil {
fmt.Println(err)
fmt.Println("Connection Error or Close. My Connections now:\r\n", sckt.wsCons)
return
}
if (messageType != websocket.BinaryMessage || sckt.onRx == nil) {
fmt.Printf("continue")
continue
}
fmt.Printf("got data! type:%d\r\n", messageType)
bCpy := make([]byte, len(binData))
copy(bCpy, binData)
packet := Datagram{}
packet.Socket = sckt
packet.Data = bCpy
//packet.Destination = conn.LocalAddr()
packet.Origin = conn.RemoteAddr()
sckt.onRx <- &packet //send away the packet
}
}
func (sckt *wsSocket) AsyncListenAndServe() {
fmt.Println("started websocket coap mirror server \"" + sckt.Uri + "\" on Port " + strconv.Itoa(sckt.Port))
http.HandleFunc(sckt.Uri, sckt.reqHandler)
http.Handle("/", http.FileServer(http.Dir(".")))
go http.ListenAndServe(":" + strconv.Itoa(sckt.Port), nil)
}
func (sckt *wsSocket) Network() string {
return "ws over TCP"
}
func (sckt *wsSocket)String() string {
return sckt.Uri + ":" + strconv.Itoa(sckt.Port)
}
func (sckt *wsSocket) LocalAddr() net.Addr {
return sckt
}