Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
Menteur
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PLN
Menteur
Commits
9d15fc59
Unverified
Commit
9d15fc59
authored
Apr 05, 2020
by
PLN (Algolia)
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat(server): MVP socketio integration
parent
fbedea4c
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
43 additions
and
5 deletions
+43
-5
app.py
server/app.py
+24
-5
ws.py
server/ws.py
+19
-0
No files found.
server/app.py
View file @
9d15fc59
from
fastapi
import
FastAPI
,
APIRouter
from
http.client
import
HTTPException
import
socketio
from
fastapi
import
FastAPI
,
APIRouter
,
HTTPException
from
starlette.websockets
import
WebSocket
from
server.model.data
import
Game
,
Player
from
server.model.data
import
Game
# Game state
from
server.model.players
import
RandomPlayer
from
server.ws
import
sio
app
=
FastAPI
()
game
=
Game
()
# Server
app
=
FastAPI
()
router
=
APIRouter
()
game
=
Game
()
# Create and mount SocketIO app
sio_asgi_app
=
socketio
.
ASGIApp
(
socketio_server
=
sio
,
other_asgi_app
=
app
)
app
.
add_route
(
"/socket.io/"
,
route
=
sio_asgi_app
,
methods
=
[
'GET'
,
'POST'
])
app
.
add_websocket_route
(
"/socket.io/"
,
sio_asgi_app
)
@router.get
(
"/"
)
...
...
@@ -16,7 +31,11 @@ async def hello_world():
@router.post
(
"/join"
)
async
def
join
(
player_name
:
str
):
game
.
players
.
append
(
Player
(
player_name
))
if
game
.
add_player
(
RandomPlayer
(
player_name
)
# TODO: Let user play
):
return
"Welcome
%
s,
%
i players currently waiting!"
%
(
player_name
,
len
(
game
.
players
))
else
:
raise
HTTPException
(
status_code
=
403
,
detail
=
f
"{player_name} already connected, choose another name."
)
@router.post
(
"/ready"
)
...
...
server/ws.py
0 → 100644
View file @
9d15fc59
import
socketio
sio
=
socketio
.
AsyncServer
(
async_mode
=
'asgi'
,
# cors_allowed_origins=','.join(config.ALLOW_ORIGIN)
)
@sio.event
def
connect
(
sid
,
environ
):
print
(
"connect "
,
sid
,
environ
)
@sio.event
async
def
chat_message
(
sid
,
data
):
print
(
"message "
,
data
)
await
sio
.
emit
(
'reply'
,
room
=
sid
)
@sio.event
def
disconnect
(
sid
):
print
(
'disconnect '
,
sid
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment