aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlonkaars <l.leblansch@gmail.com>2021-02-19 19:39:01 +0100
committerlonkaars <l.leblansch@gmail.com>2021-02-19 19:39:01 +0100
commit1787205f04c9008a753618839bc8da72708f5cab (patch)
tree7501351f6436910bc210124af45d38a2a10bc0eb
parent5f7ba73f128eddb5c3e4e11dd0025aa1c8d2f385 (diff)
working socket.io messages v2
-rw-r--r--api/game/socket.py16
-rw-r--r--pages/game.tsx17
2 files changed, 18 insertions, 15 deletions
diff --git a/api/game/socket.py b/api/game/socket.py
index fdeac73..55bff0f 100644
--- a/api/game/socket.py
+++ b/api/game/socket.py
@@ -1,12 +1,10 @@
from flask import Blueprint, request, make_response
-from flask_socketio import SocketIO, emit, disconnect, emit
+from flask_socketio import SocketIO, emit, Namespace
from game.voerbak_connector import bord
from db import cursor
import time
import json
-namespace = "/game/socket"
-
class game:
def __init__(self, game_id):
self.game_id = game_id
@@ -21,12 +19,14 @@ class game:
def run(app):
io = SocketIO(app, cors_allowed_origins="*")
- @io.on("new_move", namespace)
- def new_move(data):
- print(data)
+ namespace = "/game/socket/"
+ @io.on("connect", namespace)
+ def connect():
+ print("new connection", namespace)
- @io.on("resign", namespace)
- def resign(data):
+ @io.on("new_move")
+ def connect(data):
+ print("new_move")
print(data)
io.run(app, host="127.0.0.1", port=5000, debug=True)
diff --git a/pages/game.tsx b/pages/game.tsx
index 8728674..f49e248 100644
--- a/pages/game.tsx
+++ b/pages/game.tsx
@@ -1,11 +1,9 @@
import { CSSProperties, Component } from 'react';
-import { io as socket } from 'socket.io-client';
+import { io as socket, Socket } from 'socket.io-client';
import axios from 'axios';
import { userInfo } from '../api/api';
import * as cookies from 'react-cookies';
-var io = socket("http://localhost:2080/api/game/socket/");
-
import { NavBar } from '../components/navbar';
import { CenteredPage } from '../components/page';
import { VoerBord } from '../components/voerBord';
@@ -51,15 +49,20 @@ class VoerGame extends Component<VoerGameProps> {
constructor(props: VoerGameProps) {
super(props);
- io.on("connect", () => {
+ if (typeof document === "undefined") return;
+ this.io = socket(window.location.origin, { resource: 'api/game/socket/socket.io' });
+
+ this.io.on("connect", () => {
console.log("connect")
- io.emit("resign", {"cool": "data"});
+ this.io.emit("resign", {"cool": "data"});
})
- io.on("disconnect", () => {
+ this.io.on("disconnect", () => {
console.log("disconnect")
})
}
+ io: Socket;
+
width = 7;
height = 6;
@@ -88,7 +91,7 @@ class VoerGame extends Component<VoerGameProps> {
token: cookies.load("token"),
gameID: "fortnite"
})
- io.emit("new_move", {
+ this.io.emit("new_move", {
move: column,
token: cookies.load("token"),
gameID: "fortnite"