Last active
December 19, 2017 14:09
-
-
Save stroum/cdbf7c06ee932e0c39ba to your computer and use it in GitHub Desktop.
Go Long Polling
This file contains 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>Document</title> | |
<script> | |
function lp() { | |
var http = new XMLHttpRequest(); | |
http.open('GET', '/lp', true); | |
http.onreadystatechange = function(event) { | |
if (this.readyState == 4) { | |
if (this.status == 200) { | |
if (http.responseText.length) { | |
document.querySelector('.messages').innerHTML += '<div class="message">' + http.responseText + '</div>'; | |
down(); | |
} | |
lp(); | |
} | |
} | |
} | |
http.send(null); | |
} | |
lp(); | |
window.onload = function() { | |
down(); | |
document.getElementById("send").onclick = function() { | |
var http = new XMLHttpRequest(); | |
http.onreadystatechange = function() { | |
if (this.readyState == 4) { | |
if (this.status == 200) { | |
console.log(http.responseText); | |
} | |
} | |
} | |
msg = document.getElementById('msg').value; | |
http.open('GET', '/msg?msg=' + msg); | |
http.send(null); | |
document.getElementById('msg').value = ''; | |
return false; | |
} | |
} | |
function down() { | |
document.querySelector(".messages").scrollTop += 99999; | |
} | |
</script> | |
<style> | |
.messages { | |
overflow-y: auto; | |
height: 500px; | |
border: 1px solid #000; | |
} | |
.message { | |
background: #f5f5f5; | |
border: 1px solid #ccc; | |
padding:10px; | |
} | |
</style> | |
</head> | |
<body> | |
<div class="hello">Hello, World!</div> | |
<form action="/lp"> | |
<textarea id="msg" name="msg" rows="3" style="width: 50%;"></textarea><br> | |
<input id="send" type="submit" value="OK"> | |
</form> | |
<br> | |
<div class="chat"> | |
<div class="messages"> | |
</div> | |
</div> | |
</body> | |
</html> |
This file contains 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" | |
"html" | |
"io" | |
"log" | |
"net/http" | |
"time" | |
) | |
var lpchan = make(chan chan string) | |
func MakeLPHandler(w http.ResponseWriter, r *http.Request) { | |
myRequestChan := make(chan string) | |
select { | |
case lpchan <- myRequestChan: | |
io.WriteString(w, html.EscapeString(<-myRequestChan)) | |
case <-time.After(30 * time.Second): | |
return | |
} | |
} | |
func NewMsgHandler(w http.ResponseWriter, r *http.Request) { | |
msg := r.FormValue("msg") | |
if len(msg) > 0 { | |
for { | |
select { | |
case clientchan := <-lpchan: | |
clientchan <- msg | |
default: | |
return | |
} | |
} | |
} | |
} | |
func handler(w http.ResponseWriter, req *http.Request) { | |
/*w.Header().Set("Content-type", "text/html") | |
t, _ := template.ParseFiles("index.html") | |
t.Execute(w, nil)*/ | |
http.ServeFile(w, req, "index.html") | |
} | |
func main() { | |
fmt.Println("Starting server on http://127.0.0.1:3000/") | |
s := &http.Server{ | |
Addr: ":3000", | |
ReadTimeout: 1000 * time.Second, | |
WriteTimeout: 1000 * time.Second, | |
MaxHeaderBytes: 1 << 20, | |
} | |
http.HandleFunc("/", handler) | |
http.HandleFunc("/lp", MakeLPHandler) | |
http.HandleFunc("/msg", NewMsgHandler) | |
log.Fatal(s.ListenAndServe()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Быдлокодище :-(