-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #115 from NIAEFEUP/feature/websockets
Add WebSocket integration
- Loading branch information
Showing
5 changed files
with
59 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import uuid | ||
|
||
class SessionsServer: | ||
def __init__(self, sio): | ||
self.sio = sio | ||
|
||
def valid_token(self, token): | ||
return True | ||
|
||
def event(self, event): | ||
return self.sio.event(event) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import os | ||
import socketio | ||
|
||
from university.socket.sessionsserver import SessionsServer | ||
|
||
async_mode = None | ||
|
||
basedir = os.path.dirname(os.path.realpath(__file__)) | ||
sio = socketio.AsyncServer( | ||
async_mode='asgi', | ||
cors_allowed_origins="*", | ||
) | ||
|
||
session_server = SessionsServer(sio) | ||
|
||
@session_server.event | ||
async def connect(sid, environ, auth): | ||
if auth is None or 'token' not in auth: | ||
raise ConnectionRefusedError('Authentication failed: No token provided') | ||
elif not session_server.valid_token(auth['token']): | ||
raise ConnectionRefusedError('Authentication failed: Invalid token') | ||
else: | ||
print('Client connected') | ||
await sio.emit('my_response', {'data': 'Connected', 'count': 0}, room=sid) | ||
|
||
|
||
@session_server.event | ||
def disconnect(sid): | ||
print('Client disconnected') | ||
|
||
@session_server.event | ||
async def test(sid, environ): | ||
await sio.emit('response', 'ya') |