Added main function and debugged connection error
This commit is contained in:
parent
1e9adde8fe
commit
da4eb3123d
24
main.go
24
main.go
|
@ -3,6 +3,7 @@ package main
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"crypto/sha1"
|
"crypto/sha1"
|
||||||
|
"crypto/tls"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
@ -11,9 +12,10 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
HeaderConnection = "Connection"
|
HeaderConnection = "Connection"
|
||||||
HeaderSecWebSocketKey = "Sec-WebSocket-Key"
|
HeaderSecWebSocketAccept = "Sec-WebSocket-Accept"
|
||||||
HeaderUpgrade = "Upgrade"
|
HeaderSecWebSocketKey = "Sec-WebSocket-Key"
|
||||||
|
HeaderUpgrade = "Upgrade"
|
||||||
|
|
||||||
websocket = "websocket"
|
websocket = "websocket"
|
||||||
|
|
||||||
|
@ -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"))
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue