inital commit
This commit is contained in:
5
wsgorillatest/go.mod
Normal file
5
wsgorillatest/go.mod
Normal file
@@ -0,0 +1,5 @@
|
||||
module wsGorillatest
|
||||
|
||||
go 1.19
|
||||
|
||||
require github.com/gorilla/websocket v1.5.0 // indirect
|
||||
2
wsgorillatest/go.sum
Normal file
2
wsgorillatest/go.sum
Normal file
@@ -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=
|
||||
22
wsgorillatest/test.html
Normal file
22
wsgorillatest/test.html
Normal file
@@ -0,0 +1,22 @@
|
||||
<!-- websockets.html -->
|
||||
<input id="input" type="text" />
|
||||
<button onclick="send()">Send</button>
|
||||
<pre id="output"></pre>
|
||||
<script>
|
||||
var input = document.getElementById("input");
|
||||
var output = document.getElementById("output");
|
||||
var socket = new WebSocket("ws://localhost:8080/echo");
|
||||
|
||||
socket.onopen = function () {
|
||||
output.innerHTML += "Status: Connected\n";
|
||||
};
|
||||
|
||||
socket.onmessage = function (e) {
|
||||
output.innerHTML += "Server: " + e.data + "\n";
|
||||
};
|
||||
|
||||
function send() {
|
||||
socket.send(input.value);
|
||||
input.value = "";
|
||||
}
|
||||
</script>
|
||||
22
wsgorillatest/websockets.html
Normal file
22
wsgorillatest/websockets.html
Normal file
@@ -0,0 +1,22 @@
|
||||
<!-- websockets.html -->
|
||||
<input id="input" type="text" />
|
||||
<button onclick="send()">Send</button>
|
||||
<pre id="output"></pre>
|
||||
<script>
|
||||
var input = document.getElementById("input");
|
||||
var output = document.getElementById("output");
|
||||
var socket = new WebSocket("ws://localhost:8080/echo");
|
||||
|
||||
socket.onopen = function () {
|
||||
output.innerHTML += "Status: Connected\n";
|
||||
};
|
||||
|
||||
socket.onmessage = function (e) {
|
||||
output.innerHTML += "Server: " + e.data + "\n";
|
||||
};
|
||||
|
||||
function send() {
|
||||
socket.send(input.value);
|
||||
input.value = "";
|
||||
}
|
||||
</script>
|
||||
50
wsgorillatest/ws.go
Normal file
50
wsgorillatest/ws.go
Normal file
@@ -0,0 +1,50 @@
|
||||
// websockets.go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"log"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
)
|
||||
|
||||
var upgrader = websocket.Upgrader{
|
||||
ReadBufferSize: 1024,
|
||||
WriteBufferSize: 1024,
|
||||
CheckOrigin: func(r *http.Request) bool {
|
||||
return true
|
||||
},
|
||||
}
|
||||
|
||||
func main() {
|
||||
http.HandleFunc("/echo", func(w http.ResponseWriter, r *http.Request) {
|
||||
conn, err := upgrader.Upgrade(w, r, nil) // error ignored for sake of simplicity
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
Reference in New Issue
Block a user