Browse Source

WebSocket layer to be able to connect "web" client

Currently working with a mini irc js implem, the flow:

* PASS
* NICK
* USER
* JOIN
* PRIVMSG

works and the ping/pong timeout keep the communication open.
tags/v0.1.0
Niels Freier 9 years ago
parent
commit
6a69a65860
4 changed files with 95 additions and 1 deletions
  1. 2
    1
      ergonomadic.conf
  2. 1
    0
      irc/config.go
  3. 36
    0
      irc/server.go
  4. 56
    0
      irc/websocket.go

+ 2
- 1
ergonomadic.conf View File

@@ -1,8 +1,9 @@
1 1
 [server]
2
-name = "irc.example.com" ; required, usually a hostname
2
+name = "localhost" ; required, usually a hostname
3 3
 database = "ergonomadic.db" ; path relative to this file
4 4
 listen = "localhost:6667" ; see `net.Listen` for examples
5 5
 listen = "[::1]:6667" ; multiple `listen`s are allowed.
6
+wslisten = ":8080" ; websocket listen
6 7
 log = "debug" ; error, warn, info, debug
7 8
 motd = "motd.txt" ; path relative to this file
8 9
 password = "JDJhJDA0JHJzVFFlNXdOUXNhLmtkSGRUQVVEVHVYWXRKUmdNQ3FKVTRrczRSMTlSWGRPZHRSMVRzQmtt" ; 'test'

+ 1
- 0
irc/config.go View File

@@ -23,6 +23,7 @@ type Config struct {
23 23
 		PassConfig
24 24
 		Database string
25 25
 		Listen   []string
26
+		Wslisten string
26 27
 		Log      string
27 28
 		MOTD     string
28 29
 		Name     string

+ 36
- 0
irc/server.go View File

@@ -6,6 +6,7 @@ import (
6 6
 	"fmt"
7 7
 	"log"
8 8
 	"net"
9
+	"net/http"
9 10
 	"os"
10 11
 	"os/signal"
11 12
 	"strings"
@@ -72,6 +73,10 @@ func NewServer(config *Config) *Server {
72 73
 		server.listen(addr)
73 74
 	}
74 75
 
76
+	if config.Server.Wslisten != "" {
77
+		server.wslisten(config.Server.Wslisten)
78
+	}
79
+
75 80
 	signal.Notify(server.signals, SERVER_SIGNALS...)
76 81
 
77 82
 	return server
@@ -203,6 +208,37 @@ func (s *Server) listen(addr string) {
203 208
 	}()
204 209
 }
205 210
 
211
+//
212
+// websocket listen goroutine
213
+//
214
+
215
+func (s *Server) wslisten(addr string) {
216
+	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
217
+		if r.Method != "GET" {
218
+			Log.error.Printf("%s method not allowed", s)
219
+			return
220
+		}
221
+		ws, err := upgrader.Upgrade(w, r, nil)
222
+		if err != nil {
223
+			Log.error.Printf("%s websocket upgrade error: %s", s, err)
224
+			return
225
+		}
226
+
227
+		wsc := WSContainer{
228
+			conn: ws,
229
+		}
230
+
231
+		s.newConns <- wsc
232
+	})
233
+	go func() {
234
+		Log.info.Printf("%s listening on %s", s, addr)
235
+		err := http.ListenAndServe(addr, nil)
236
+		if err != nil {
237
+			Log.error.Printf("%s listenAndServe error: %s", s, err)
238
+		}
239
+	}()
240
+}
241
+
206 242
 //
207 243
 // server functionality
208 244
 //

+ 56
- 0
irc/websocket.go View File

@@ -0,0 +1,56 @@
1
+package irc
2
+
3
+import (
4
+	"github.com/gorilla/websocket"
5
+	"net"
6
+	"net/http"
7
+	"time"
8
+)
9
+
10
+var upgrader = websocket.Upgrader{
11
+	ReadBufferSize:  1024,
12
+	WriteBufferSize: 1024,
13
+	CheckOrigin:     func(r *http.Request) bool { return true },
14
+}
15
+
16
+type WSContainer struct {
17
+	conn *websocket.Conn
18
+}
19
+
20
+func (this WSContainer) Close() error {
21
+	return this.conn.Close()
22
+}
23
+
24
+func (this WSContainer) LocalAddr() net.Addr {
25
+	return this.conn.LocalAddr()
26
+}
27
+
28
+func (this WSContainer) RemoteAddr() net.Addr {
29
+	return this.conn.RemoteAddr()
30
+}
31
+
32
+func (this WSContainer) Read(msg []byte) (int, error) {
33
+	_, tmp, err := this.conn.ReadMessage()
34
+	str := (string)(tmp)
35
+	n := copy(msg, ([]byte)(str+CRLF+CRLF))
36
+	return n, err
37
+}
38
+
39
+func (this WSContainer) Write(msg []byte) (int, error) {
40
+	err := this.conn.WriteMessage(1, msg)
41
+	return len(msg), err
42
+}
43
+
44
+func (this WSContainer) SetDeadline(t time.Time) error {
45
+	err := this.conn.SetWriteDeadline(t)
46
+	err = this.conn.SetReadDeadline(t)
47
+	return err
48
+}
49
+
50
+func (this WSContainer) SetReadDeadline(t time.Time) error {
51
+	return this.conn.SetReadDeadline(t)
52
+}
53
+
54
+func (this WSContainer) SetWriteDeadline(t time.Time) error {
55
+	return this.conn.SetWriteDeadline(t)
56
+}

Loading…
Cancel
Save