simple websocket example with golang
<html> | |
<head> | |
<title>WebSocket demo</title> | |
</head> | |
<body> | |
<div> | |
<form> | |
<label for="numberfield">Number</label> | |
<input type="text" id="numberfield" placeholder="12"/><br /> | |
<button type="button" id="sendBtn">Send</button> | |
</form> | |
</div> | |
<div id="container"></div> | |
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> | |
<script type="text/javascript"> | |
$(function () { | |
var ws; | |
if (window.WebSocket === undefined) { | |
$("#container").append("Your browser does not support WebSockets"); | |
return; | |
} else { | |
ws = initWS(); | |
} | |
function initWS() { | |
var socket = new WebSocket("ws://localhost:8080/ws"), | |
container = $("#container") | |
socket.onopen = function() { | |
container.append("<p>Socket is open</p>"); | |
}; | |
socket.onmessage = function (e) { | |
container.append("<p> Got some shit:" + e.data + "</p>"); | |
} | |
socket.onclose = function () { | |
container.append("<p>Socket closed</p>"); | |
} | |
return socket; | |
} | |
$("#sendBtn").click(function (e) { | |
e.preventDefault(); | |
ws.send(JSON.stringify({ Num: parseInt($("#numberfield").val()) })); | |
}); | |
}); | |
</script> | |
</body> | |
</html> |
package main | |
import ( | |
"fmt" | |
"github.com/gorilla/websocket" | |
"io/ioutil" | |
"net/http" | |
) | |
type msg struct { | |
Num int | |
} | |
func main() { | |
http.HandleFunc("/ws", wsHandler) | |
http.HandleFunc("/", rootHandler) | |
panic(http.ListenAndServe(":8080", nil)) | |
} | |
func rootHandler(w http.ResponseWriter, r *http.Request) { | |
content, err := ioutil.ReadFile("index.html") | |
if err != nil { | |
fmt.Println("Could not open file.", err) | |
} | |
fmt.Fprintf(w, "%s", content) | |
} | |
func wsHandler(w http.ResponseWriter, r *http.Request) { | |
if r.Header.Get("Origin") != "http://"+r.Host { | |
http.Error(w, "Origin not allowed", 403) | |
return | |
} | |
conn, err := websocket.Upgrade(w, r, w.Header(), 1024, 1024) | |
if err != nil { | |
http.Error(w, "Could not open websocket connection", http.StatusBadRequest) | |
} | |
go echo(conn) | |
} | |
func echo(conn *websocket.Conn) { | |
for { | |
m := msg{} | |
err := conn.ReadJSON(&m) | |
if err != nil { | |
fmt.Println("Error reading json.", err) | |
} | |
fmt.Printf("Got message: %#v\n", m) | |
if err = conn.WriteJSON(m); err != nil { | |
fmt.Println(err) | |
} | |
} | |
} |
This comment has been minimized.
This comment has been minimized.
see x/net/websocket |
This comment has been minimized.
This comment has been minimized.
you need a return if websocket.Upgrade returns an error. (insert at line 37) |
This comment has been minimized.
This comment has been minimized.
Not sure why peeps are trying to correct your code. This worked perfectly for me right out of the box. It took me forever to find a good example of using websockets in a golang based web app. Thank you!!!! |
This comment has been minimized.
This comment has been minimized.
hi , please how to send 2 or many json files ??? |
This comment has been minimized.
This comment has been minimized.
Thanks a ton! @tmichel |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.
Index.html, line 29, use: "ws://" + location.host + "/ws"