-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathws_forwarder.go
58 lines (48 loc) · 1.17 KB
/
ws_forwarder.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
package tme
import (
"context"
"github.com/dnsge/twitch-mobile-emotes/app"
"github.com/dnsge/twitch-mobile-emotes/session"
"github.com/gorilla/websocket"
"log"
"net/http"
)
const (
twitchIrcWsURL = "wss://irc-ws.chat.twitch.tv:443"
)
func ConnectToTwitchIrc(ctx context.Context) (*websocket.Conn, error) {
dialer := websocket.Dialer{}
conn, _, err := dialer.DialContext(ctx, twitchIrcWsURL, http.Header{})
return conn, err
}
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
CheckOrigin: func(r *http.Request) bool {
// trust all origins
return true
},
}
type WsForwarder struct {
ctx *app.Context
}
func NewWsForwarder(ctx *app.Context) *WsForwarder {
return &WsForwarder{
ctx: ctx,
}
}
func (f *WsForwarder) HandleWsConnection(w http.ResponseWriter, r *http.Request) {
twitchConn, err := ConnectToTwitchIrc(f.ctx.Config.Context)
if err != nil {
log.Printf("Failed to connect to Twitch IRC server: %v\n", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Println(err)
return
}
defer conn.Close()
session.RunWsSession(conn, twitchConn, f.ctx)
}