Last active
May 13, 2016 16:01
-
-
Save kmazanec/b2f31a220655bf44ecc7daf30cf096a1 to your computer and use it in GitHub Desktop.
Web socket chat server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<!-- Forked from a JSFiddle, available here: http://jsfiddle.net/rxcwyypv/2/ --> | |
<head> | |
<title>WebSocket Echo Test</title> | |
<script> | |
function init() { | |
websocket = new WebSocket("ws://localhost:8080/read"); | |
websocket.onopen = function() { document.getElementById("output").innerHTML += "<p>> CONNECTED</p>"; }; | |
websocket.onmessage = function(evt) { document.getElementById("output").innerHTML += "<p style='color: blue;'>> RESPONSE: " + evt.data + "</p>"; }; | |
websocket.onerror = function(evt) { document.getElementById("output").innerHTML += "<p style='color: red;'>> ERROR: " + evt.data + "</p>"; }; | |
} | |
function sendMessage(message) { | |
document.getElementById("output").innerHTML += "<p>> SENT: " + message + "</p>"; | |
websocket.send(message); | |
} | |
window.addEventListener("load", init, false); | |
</script> | |
</head> | |
<body> | |
<input onkeypress="if(this.value) {if (window.event.keyCode == 13) { sendMessage(this.value); this.value = null; }}"/> | |
<div id="output"></div> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"net/http" | |
"golang.org/x/net/websocket" | |
) | |
func readFromWebsocket(ws *websocket.Conn) (string, error) { | |
receivedtext := make([]byte, 100) | |
n, err := ws.Read(receivedtext) | |
if err != nil { | |
fmt.Printf("Received: %d bytes\n", n) | |
return "", err | |
} | |
s := string(receivedtext[:n]) | |
return s, nil | |
} | |
func readHandler(ws *websocket.Conn, us *users) { | |
// TODO: setting up a mutex so that the reader and writer do work at the same time | |
username, err := readFromWebsocket(ws) | |
if err != nil { | |
ws.Close() | |
return | |
} | |
wsType, err := readFromWebsocket(ws) // it should be "r" or "w" | |
if err != nil { | |
ws.Close() | |
return | |
} | |
switch wsType { | |
case "r": | |
if val, ok := (*us)[username]; ok { | |
fmt.Println("This is a reader for", username) | |
case "w": | |
fmt.Println("This is a writer for", username) | |
default: | |
ws.Close() | |
return | |
} | |
for { | |
s, err := readFromWebsocket(ws) | |
if err != nil { | |
ws.Close() | |
return | |
} | |
fmt.Printf("Received: %s\n", s) | |
//io.Copy(ws, ws) | |
//fmt.Printf("Sent: %s\n",s) | |
} | |
} | |
func main() { | |
m := make(users) | |
http.Handle("/read", websocket.Handler(func(ws *websocket.Conn) { | |
readHandler(ws, &m) | |
})) | |
err := http.ListenAndServe(":8080", nil) | |
if err != nil { | |
panic("Error: " + err.Error()) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "golang.org/x/net/websocket" | |
type users map[string]user | |
type user struct { | |
Reader *websocket.Conn | |
Writer *websocket.Conn | |
} | |
type message struct { | |
Author user | |
Body string | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment