From fd7bd1c0e1fd3f6b72aad0adf192b421235e2d91 Mon Sep 17 00:00:00 2001 From: Askill Date: Thu, 26 May 2022 14:14:36 +0200 Subject: [PATCH] init --- .gitignore | 4 +++ go/server.go | 71 +++++++++++++++++++++++++++++++++++++++++ go/util.go | 19 +++++++++++ python/clients.py | 37 +++++++++++++++++++++ python/requirements.txt | 3 ++ 5 files changed, 134 insertions(+) create mode 100644 go/server.go create mode 100644 go/util.go create mode 100644 python/clients.py create mode 100644 python/requirements.txt diff --git a/.gitignore b/.gitignore index b6e4761..94babdb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,7 @@ +**/*.mod +**/*.sum + + # Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] diff --git a/go/server.go b/go/server.go new file mode 100644 index 0000000..ae06188 --- /dev/null +++ b/go/server.go @@ -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)) +} diff --git a/go/util.go b/go/util.go new file mode 100644 index 0000000..33b6e44 --- /dev/null +++ b/go/util.go @@ -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 +} diff --git a/python/clients.py b/python/clients.py new file mode 100644 index 0000000..c063ca4 --- /dev/null +++ b/python/clients.py @@ -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()) diff --git a/python/requirements.txt b/python/requirements.txt new file mode 100644 index 0000000..d5fb6a7 --- /dev/null +++ b/python/requirements.txt @@ -0,0 +1,3 @@ +websockets +numpy +matplotlib \ No newline at end of file