This commit is contained in:
Askill 2022-05-26 14:14:36 +02:00
parent 72ff0410a1
commit fd7bd1c0e1
5 changed files with 134 additions and 0 deletions

4
.gitignore vendored
View File

@ -1,3 +1,7 @@
**/*.mod
**/*.sum
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]

71
go/server.go Normal file
View File

@ -0,0 +1,71 @@
// Copyright 2015 The Gorilla WebSocket Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build ignore
// +build ignore
package main
import (
"encoding/json"
"flag"
"log"
"net/http"
"time"
"github.com/gorilla/websocket"
)
var addr = flag.String("addr", "localhost:8080", "http service address")
var upgrader = websocket.Upgrader{
ReadBufferSize: 2048,
WriteBufferSize: 2048,
}
type Pixel struct {
X uint16 `json:"x"`
Y uint16 `json:"y"`
Color uint8 `json:"color"`
Timestamp int64 `json:"timestamp"`
UserID uint64 `json:"userid"`
}
func JsonToStruct(input []byte) Pixel {
pixel := Pixel{}
json.Unmarshal(input, &pixel)
return pixel
}
func serve(w http.ResponseWriter, r *http.Request) {
c, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Print("error while upgrading", err)
return
}
defer c.Close()
for {
mt, message, err := c.ReadMessage()
if err != nil {
//log.Println("read:", err)
break
}
pixel := JsonToStruct(message)
tm := time.Unix(pixel.Timestamp, 0)
log.Println(tm)
err = c.WriteMessage(mt, message)
if err != nil {
//log.Println("write:", err)
break
}
}
}
func main() {
flag.Parse()
log.SetFlags(0)
http.HandleFunc("/", serve)
log.Fatal(http.ListenAndServe(*addr, nil))
}

19
go/util.go Normal file
View File

@ -0,0 +1,19 @@
package main
import (
"encoding/json"
)
type Pixel struct {
X uint16 `json:"x"`
Y uint16 `json:"y"`
Color uint8 `json:"color"`
Timestamp uint64 `json:"timestamp"`
UserID uint64 `json:"userid"`
}
func JsonToStruct(input []byte) Pixel {
pixel := Pixel{}
json.Unmarshal(input, &pixel)
return pixel
}

37
python/clients.py Normal file
View File

@ -0,0 +1,37 @@
#!/usr/bin/env python
import asyncio
from dataclasses import dataclass
import datetime
import json
import time
import websockets
@dataclass
class pixel:
x: int
y: int
color: int
timestamp: int
userid: int
async def main():
message = pixel(
x=0,
y=1,
color=0,
timestamp=int(time.time()),
userid=0,
)
async with websockets.connect("ws://localhost:8080") as websocket:
print(message)
await websocket.send(json.dumps(message.__dict__))
await websocket.recv()
if __name__ == "__main__":
asyncio.get_event_loop().run_until_complete(main())

3
python/requirements.txt Normal file
View File

@ -0,0 +1,3 @@
websockets
numpy
matplotlib