Added main function and debugged connection error

This commit is contained in:
Samuel Hawksby-Robinson 2024-06-12 14:54:18 +01:00
parent 1e9adde8fe
commit da4eb3123d
No known key found for this signature in database
GPG Key ID: 0A38D3BB2983AE5B
1 changed files with 20 additions and 4 deletions

24
main.go
View File

@ -3,6 +3,7 @@ package main
import (
"bufio"
"crypto/sha1"
"crypto/tls"
"encoding/base64"
"fmt"
"log"
@ -11,9 +12,10 @@ import (
)
const (
HeaderConnection = "Connection"
HeaderSecWebSocketKey = "Sec-WebSocket-Key"
HeaderUpgrade = "Upgrade"
HeaderConnection = "Connection"
HeaderSecWebSocketAccept = "Sec-WebSocket-Accept"
HeaderSecWebSocketKey = "Sec-WebSocket-Key"
HeaderUpgrade = "Upgrade"
websocket = "websocket"
@ -34,7 +36,7 @@ func handleWebSocket(w http.ResponseWriter, r *http.Request) {
headers := http.Header{}
headers.Set(HeaderUpgrade, websocket)
headers.Set(HeaderConnection, HeaderUpgrade)
headers.Set(HeaderSecWebSocketKey, secWebSocketAccept)
headers.Set(HeaderSecWebSocketAccept, secWebSocketAccept)
for k, v := range headers {
w.Header()[k] = v
@ -151,3 +153,17 @@ func handleWriteWebSocketData(conn net.Conn, messageType int, payload []byte) er
return nil
}
func main() {
http.HandleFunc("/ws", handleWebSocket)
server := &http.Server{
Addr: ":8443",
TLSConfig: &tls.Config{
MinVersion: tls.VersionTLS12,
},
}
log.Println("WebSocket server started on wss://localhost:8443/ws")
log.Fatal(server.ListenAndServeTLS("cert.pem", "key.pem"))
}