-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebsocket.py
executable file
·144 lines (109 loc) · 4.82 KB
/
websocket.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
from sanic import Sanic, exceptions
from sanic.response import redirect
from threading import Thread
import asyncio
import socketio
import os
from time import sleep
from game_root import Game
# mgr = socketio.AsyncRedisManager('redis://')
cookie = {'name': "SESSID", "SameSite": "None", "Secure": True, }
sio = socketio.asyncio_server.AsyncServer(async_mode='sanic', cors_allowed_origins=os.environ.get('ORIGINS'),
logger=False,
engineio_logger=False,
# cookie=cookie, # works but not really?
) # ,client_manager=mgr)
print("Async server started, accepting connections from", os.environ.get('ORIGINS'), "on port", os.environ.get('PORT'))
goFast = Sanic(name="GhostSystem Local")
sio.attach(goFast)
game = Game()
@goFast.get('/')
async def handler(request):
return redirect("https://ghostsystem-web.herokuapp.com/")
@goFast.listener('after_server_start')
def after_server_start(sanic, loop):
sanic.background_task = loop.create_task(game.game_loop(sio, sanic, loop))
pass
@goFast.listener('before_server_stop')
async def before_server_stop(sanic, loop):
print("got nice stop, trying to stop the background thread")
sanic.background_task.cancel()
await sanic.background_task
print("done stopping background thread")
for task in asyncio.Task.all_tasks(loop):
if task.get_coro().cr_code.co_name != "before_server_stop":
task.cancel() # cancel all tasks except this one
for eio_session in sio.eio.sockets.keys(): # boot everyone out so we can shutdown
session = sio.manager.sid_from_eio_sid(eio_session, "/")
await sio.disconnect(session)
@sio.event
async def my_event(sid: str, message: dict):
await sio.emit('my_response', {'data': message['data']}, room=sid)
@sio.event
async def map_update(sid: str, message): # this is only to catch map_update thrown from client
await sio.emit('map', {'data': message['data']}, room=sid)
@sio.event
async def my_broadcast_event(sid: str, message: dict):
await sio.emit('my_response', {'data': message['data']})
@sio.event
async def join(sid: str, message: dict):
sio.enter_room(sid, message['room'])
await sio.emit('my_response', {'data': 'Entered room: ' + message['room']},
room=sid)
@sio.event
async def leave(sid: str, message: dict):
sio.leave_room(sid, message['room'])
await sio.emit('my_response', {'data': 'Left room: ' + message['room']},
room=sid)
@sio.event
async def close_room(sid: str, message: dict):
await sio.emit('my_response',
{'data': 'Room ' + message['room'] + ' is closing.'},
room=message['room'])
await sio.close_room(message['room'])
@sio.event
async def my_room_event(sid: str, message: dict):
await sio.emit('my_response', {'data': message['data']},
room=message['room'])
@sio.event
async def disconnect_request(sid: str):
try:
await sio.disconnect(sid)
except exceptions.ServerError:
pass
@sio.event
async def connect(sid: str, environ):
print("Client", sid, "connected")
# await sio.emit('ask_for_session', {}, room=sid)
@sio.event
def disconnect(sid: str):
game.client_disconnect(sid)
print('Client disconnected', sid)
@sio.event
async def new_character(sid: str, message: dict):
ent = game.new_character(sid, message)
if ent:
await sio.emit('new_character',
{'message': "Character " + message['characterName'] + " was created for user " + message[
'username'],
'characterName': message['characterName'], 'entity': ent}, room=sid)
else:
await sio.emit('new_character',
{'message': "Could not create Character " + message['characterName'],
'characterName': None, 'entity': ent}, room=sid)
@sio.event
async def register(sid: str, message: dict):
success = game.register(sid, message)
if success:
await sio.emit('register', {'message': "User " + message['username'] + " was created", 'success': True,
'auth_token': success[0], 'username': success[1]}, room=sid)
else:
await sio.emit('register', {'message': "Registration Failed", 'success': success}, room=sid)
@sio.event
async def authenticate(sid: str, message: dict):
success = game.authenticate(sid, message)
if success:
await sio.emit('authenticate', {'message': "Welcome back " + message['username'], 'success': True,
'auth_token': success[0], 'username': success[1]}, room=sid)
else:
await sio.emit('authenticate', {'message': "Authentication Failed", 'success': success}, room=sid)