diff --git a/go/server.go b/go/server.go index 5a2b70c..95c1e33 100644 --- a/go/server.go +++ b/go/server.go @@ -44,13 +44,11 @@ func get(w http.ResponseWriter, r *http.Request) { defer c.Close() ticker := time.NewTicker(1 * time.Second) - - var refImage = GetImage(img.width, img.height) var tmpImage = GetImage(img.width, img.height) for range ticker.C { - copy(refImage.pixels, img.pixels) - diff := tmpImage.GetDiff(&refImage) + + diff := tmpImage.GetDiff(&img) for i := 0; i < int(diff.width*diff.height); i++ { pix := diff.pixels[i] if pix.pixel.UserID != 0 { @@ -70,7 +68,7 @@ func get(w http.ResponseWriter, r *http.Request) { if err := c.WriteMessage(websocket.PingMessage, []byte{}); err != nil { return } - copy(tmpImage.pixels, refImage.pixels) + copy(tmpImage.pixels, img.pixels) } } diff --git a/go/util.go b/go/util.go index 31e8c7b..f7bd4db 100644 --- a/go/util.go +++ b/go/util.go @@ -53,7 +53,7 @@ func (img *image) SetPixel(message Message) int { fmt.Printf("User %d tried accessing out of bounds \n", message.UserID) return 1 } - if message.Color >= 16 || message.Color < 0 { + if message.Color >= 255 || message.Color < 0 { fmt.Printf("User %d tried setting non existent color \n", message.UserID) return 1 } diff --git a/python/clients.py b/python/clients.py index 7a48361..06504ba 100644 --- a/python/clients.py +++ b/python/clients.py @@ -13,6 +13,8 @@ import numpy as np import websockets import cv2 +import matplotlib.image as mpimg + @dataclass class pixel: @@ -22,13 +24,15 @@ class pixel: timestamp: int userid: int -async def sender(): +async def sender(img): async with websockets.connect("ws://localhost:8080/set") as websocket: while True: + rx = random.randint(0, 999) + ry = random.randint(0, 999) message = pixel( - x=random.randint(0, 999), - y=random.randint(0, 999), - color=random.randint(0,15), + x=rx, + y=ry, + color=int(img[rx][ry][0]*255), timestamp=int(time.time()), userid=1, ) @@ -37,7 +41,7 @@ async def sender(): if succ == "1": print(message, "was not set") - await asyncio.sleep(0.1) + await asyncio.sleep(0.01) async def client(): image = np.zeros(shape=[1000, 1000, 3], dtype=np.uint8) @@ -50,15 +54,18 @@ async def client(): while True: i+=1 x = pixel(**json.loads(await websocket.recv())) - image[x.x][x.y] = ([y*255 for y in colors[x.color]]) - #if i% 4000 == 0: - # cv2.imshow("changes x", image) - # cv2.waitKey(10) & 0XFF + #image[x.x][x.y] = ([y*255 for y in colors[x.color]]) + image[x.x][x.y] = ((x.color, x.color, x.color)) + if i% 1000 == 0: + print("showing") + cv2.imshow("changes x", image) + cv2.waitKey(10) & 0XFF await websocket.send("1") #print(i, x) async def main(): - coros = [sender() for _ in range(500)] + img=mpimg.imread('logo.png') + coros = [sender(img) for _ in range(10)] coros.append(client()) returns = await asyncio.gather(*coros)