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

18
main.go
View File

@ -3,6 +3,7 @@ package main
import ( import (
"bufio" "bufio"
"crypto/sha1" "crypto/sha1"
"crypto/tls"
"encoding/base64" "encoding/base64"
"fmt" "fmt"
"log" "log"
@ -12,6 +13,7 @@ import (
const ( const (
HeaderConnection = "Connection" HeaderConnection = "Connection"
HeaderSecWebSocketAccept = "Sec-WebSocket-Accept"
HeaderSecWebSocketKey = "Sec-WebSocket-Key" HeaderSecWebSocketKey = "Sec-WebSocket-Key"
HeaderUpgrade = "Upgrade" HeaderUpgrade = "Upgrade"
@ -34,7 +36,7 @@ func handleWebSocket(w http.ResponseWriter, r *http.Request) {
headers := http.Header{} headers := http.Header{}
headers.Set(HeaderUpgrade, websocket) headers.Set(HeaderUpgrade, websocket)
headers.Set(HeaderConnection, HeaderUpgrade) headers.Set(HeaderConnection, HeaderUpgrade)
headers.Set(HeaderSecWebSocketKey, secWebSocketAccept) headers.Set(HeaderSecWebSocketAccept, secWebSocketAccept)
for k, v := range headers { for k, v := range headers {
w.Header()[k] = v w.Header()[k] = v
@ -151,3 +153,17 @@ func handleWriteWebSocketData(conn net.Conn, messageType int, payload []byte) er
return nil 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"))
}