47 lines
757 B
Go
47 lines
757 B
Go
package main
|
|
|
|
import (
|
|
"log"
|
|
"net"
|
|
"net/http"
|
|
"strconv"
|
|
"time"
|
|
)
|
|
|
|
type Message struct {
|
|
Type string `json:"type"`
|
|
Content string `json:"content"`
|
|
}
|
|
|
|
func sessionHandler(w http.ResponseWriter, r *http.Request, conn net.Conn) {
|
|
log.Println("Got sessions websocket connection")
|
|
go func() {
|
|
for {
|
|
b := make([]byte, 1024)
|
|
log.Printf("Reading from %v", conn)
|
|
_, err := conn.Read(b)
|
|
if err != nil {
|
|
return
|
|
}
|
|
}
|
|
}()
|
|
i := 0
|
|
for {
|
|
time.Sleep(1 * time.Second)
|
|
message := `
|
|
<div id="mycontent">
|
|
New data: ` + strconv.Itoa(i) + `
|
|
</div>
|
|
`
|
|
_, err := conn.Write([]byte(message))
|
|
if err == nil {
|
|
_, err = conn.Write([]byte("\n"))
|
|
}
|
|
if err != nil {
|
|
log.Printf("ERROR sending message: %v", err)
|
|
return
|
|
}
|
|
i++
|
|
}
|
|
}
|