-
-
Save febeling/5253901 to your computer and use it in GitHub Desktop.
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
$(function() { | |
var source = new EventSource('/events'); | |
source.onopen = function (event) { | |
console.log("eventsource connection open"); | |
}; | |
source.onerror = function() { | |
if (event.target.readyState === 0) { | |
console.log("reconnecting to eventsource"); | |
} else { | |
console.log("eventsource error"); | |
} | |
}; | |
source.onmessage = function(event) { | |
$('<div>', { | |
text: event.data, | |
css: { | |
display: "none" | |
} | |
}) | |
.prependTo("#messages") | |
.show("fast") | |
; | |
}; | |
}); |
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 ( | |
"encoding/json" | |
"fmt" | |
"net/http" | |
) | |
const ( | |
msgBuf = 10 | |
) | |
type Message struct { | |
Text string | |
} | |
type Response map[string]interface{} | |
func (r Response) String() (s string) { | |
b, err := json.Marshal(r) | |
if err != nil { | |
s = "" | |
return | |
} | |
s = string(b) | |
return | |
} | |
type Broker struct { | |
subscribers map[chan []byte]bool | |
} | |
func (b *Broker) Subscribe() chan []byte { | |
ch := make(chan []byte, msgBuf) | |
b.subscribers[ch] = true | |
return ch | |
} | |
func (b *Broker) Unsubscribe(ch chan []byte) { | |
delete(b.subscribers, ch) | |
} | |
func (b *Broker) Publish(msg []byte) { | |
for ch := range b.subscribers { | |
ch <- msg | |
} | |
} | |
func NewBroker() *Broker { | |
return &Broker{make(map[chan []byte]bool)} | |
} | |
var msgBroker *Broker | |
func messageHandler(w http.ResponseWriter, r *http.Request) { | |
if r.Method != "POST" { | |
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) | |
return | |
} | |
if r.Header.Get("Content-Type")[:16] != "application/json" { | |
http.Error(w, "Content-Type must be application/json", http.StatusNotAcceptable) | |
return | |
} | |
var m Message | |
dec := json.NewDecoder(r.Body) | |
if err := dec.Decode(&m); err != nil { | |
http.Error(w, err.Error(), http.StatusInternalServerError) | |
return | |
} | |
msgBroker.Publish([]byte(m.Text)) | |
if r.Header.Get("Accept") == "application/json" { | |
w.Header().Set("Content-Type", "application/json") | |
fmt.Fprint(w, Response{"success": true, "message": "OK"}) | |
return | |
} | |
fmt.Fprintln(w, "OK") | |
} | |
func timerEventSource(w http.ResponseWriter, r *http.Request) { | |
f, ok := w.(http.Flusher) | |
if !ok { | |
http.Error(w, "streaming unsupported", http.StatusInternalServerError) | |
return | |
} | |
w.Header().Set("Content-Type", "text/event-stream") | |
w.Header().Set("Cache-Control", "no-cache") | |
w.Header().Set("Connection", "keep-alive") | |
ch := msgBroker.Subscribe() | |
defer msgBroker.Unsubscribe(ch) | |
for { | |
msg := <-ch | |
fmt.Fprintf(w, "data: Message: %s\n\n", msg) | |
f.Flush() | |
} | |
} | |
func main() { | |
msgBroker = NewBroker() | |
http.HandleFunc("/update", messageHandler) | |
http.HandleFunc("/events", timerEventSource) | |
http.Handle("/", http.FileServer(http.Dir("static"))) | |
err := http.ListenAndServe("localhost:8888", nil) | |
if err != nil { | |
panic(err) | |
} | |
} |
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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<title>EventSource Demo</title> | |
<link rel="stylesheet" href="style.css" type="text/css"> | |
<script src="http://code.jquery.com/jquery-1.8.2.min.js"></script> | |
</head> | |
<body> | |
<div class="container"> | |
<h1>Messages</h1> | |
<div id="messages"></div> | |
</div> | |
<script src="app.js"></script> | |
</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
* { | |
box-sizing: border-box; | |
} | |
body { | |
background-color: #eee; | |
font: 100%/1.5 "Source Sans Pro", "Helvetica Neue", Helvetica, sans-serif; | |
padding-top: 2em; | |
} | |
.container { | |
max-width: 720px; | |
margin: 0 auto; | |
padding: 20px; | |
background-color: #fff; | |
color: #333; | |
border: 1px solid #ddd; | |
} | |
h1 { | |
margin-top: 0; | |
} | |
#messages { | |
max-height: 480px; | |
overflow-y: auto; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment