diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c5e82d7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +bin \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..1fd453a --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module kbca/sudoku + +go 1.17 + +require github.com/gorilla/websocket v1.5.0 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..e5a03d4 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc= +github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= diff --git a/server.go b/server.go new file mode 100644 index 0000000..57ea095 --- /dev/null +++ b/server.go @@ -0,0 +1,41 @@ +package main + +import ( + "fmt" + "net/http" + + "github.com/gorilla/websocket" +) + +var upgrader = websocket.Upgrader{ + ReadBufferSize: 1024, + WriteBufferSize: 1024, +} + +func main() { + http.HandleFunc("/echo", func(w http.ResponseWriter, r *http.Request) { + conn, _ := upgrader.Upgrade(w, r, nil) // error ignored for sake of simplicity + + for { + // Read message from browser + msgType, msg, err := conn.ReadMessage() + if err != nil { + return + } + + // Print the message to the console + fmt.Printf("%s sent: %s\n", conn.RemoteAddr(), string(msg)) + + // Write message back to browser + if err = conn.WriteMessage(msgType, msg); err != nil { + return + } + } + }) + + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + http.ServeFile(w, r, "websockets.html") + }) + + http.ListenAndServe(":8080", nil) +} diff --git a/websockets.html b/websockets.html new file mode 100644 index 0000000..4159134 --- /dev/null +++ b/websockets.html @@ -0,0 +1,21 @@ + + +
+ \ No newline at end of file