Skip to content

Commit f55ee3c

Browse files
committed
made rooms on websocket server
1 parent 594ef30 commit f55ee3c

File tree

4 files changed

+74
-55
lines changed

4 files changed

+74
-55
lines changed

backend/websocket/server.py

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,55 +7,76 @@
77

88

99
class Server:
10-
clients = set()
10+
# {
11+
# 'board_url': set(...)
12+
# }
13+
clients = {}
1114

12-
async def register(self, ws: WebSocketServerProtocol) -> None:
13-
self.clients.add(ws)
15+
async def register(self, ws: WebSocketServerProtocol, board_url: str) -> None:
16+
if board_url not in self.clients:
17+
self.clients[board_url] = set()
18+
19+
self.clients[board_url].add(ws)
1420
logging.info("{} connected".format(ws.remote_address))
1521

16-
async def unregister(self, ws: WebSocketServerProtocol) -> None:
22+
async def unregister(self, ws: WebSocketServerProtocol, board_url: str) -> None:
1723
await ws.close()
18-
self.clients.remove(ws)
24+
self.clients[board_url].remove(ws)
1925
logging.info("{} disconnected".format(ws.remote_address))
2026

27+
if not self.clients[board_url]:
28+
self.clients.pop(board_url)
29+
2130
async def main_handler(self, ws: WebSocketServerProtocol, uri: str) -> None:
2231

2332
logging.info("uri: {} handled from {}".format(uri, ws.remote_address))
24-
if uri != "/board":
33+
34+
if not uri.startswith("/board/"):
2535
await ws.close()
2636

37+
board_url = uri[7:]
38+
2739
if ws not in self.clients:
28-
await self.register(ws)
40+
await self.register(ws, board_url)
2941

3042
try:
31-
await self.distribute(ws)
43+
await self.distribute(ws, board_url)
3244
except:
3345
pass
3446
finally:
35-
await self.unregister(ws)
47+
await self.unregister(ws, board_url)
3648

37-
async def distribute(self, ws: WebSocketServerProtocol) -> None:
49+
async def distribute(self, ws: WebSocketServerProtocol, board_url: str) -> None:
3850
async for message in ws:
39-
await self.send_to_clients(message, ws)
51+
await self.send_to_clients(message, ws, board_url)
4052
logging.info(
4153
"received message: {} from {}".format(message, ws.remote_address)
4254
)
4355

4456
async def send_to_clients(
45-
self, message: str, current_ws: WebSocketServerProtocol
57+
self, message: str, current_ws: WebSocketServerProtocol, board_url: str
4658
) -> None:
47-
if self.clients and len(self.clients) > 1:
59+
if (
60+
board_url in self.clients
61+
and self.clients[board_url]
62+
and len(self.clients[board_url]) > 1
63+
):
4864
await asyncio.wait(
4965
[
5066
client.send(message)
51-
for client in self.clients
67+
for client in self.clients[board_url]
5268
if client != current_ws
5369
]
5470
)
5571

5672

57-
server = Server()
58-
start_server = websockets.serve(server.main_handler, "0.0.0.0", 8001)
59-
loop = asyncio.get_event_loop()
60-
loop.run_until_complete(start_server)
61-
loop.run_forever()
73+
def start():
74+
server = Server()
75+
start_server = websockets.serve(server.main_handler, "0.0.0.0", 8001)
76+
loop = asyncio.get_event_loop()
77+
loop.run_until_complete(start_server)
78+
loop.run_forever()
79+
80+
81+
if __name__ == "__main__":
82+
start()

docker-compose.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ services:
44
api:
55
build: ./backend/api
66
restart: always
7-
# command: gunicorn entry:app --bind 0.0.0.0:8000 --reload
8-
command: python3 entry.py
7+
command: gunicorn entry:app --bind 0.0.0.0:8000 --reload
8+
# command: python3 entry.py
99
ports:
1010
- 8000:8000
1111
volumes:

frontend/components/Toolbar.js

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ import {changeBrushColor, changeBrushWidth} from '../store/toolbar/actions';
55
import { clearBoard } from '../store/board/actions';
66
import * as actions from '../store/toolbar/constants';
77

8-
class Toolbar extends React.Component{
9-
saveCanvas = (event) => {
8+
export const Toolbar = props => {
9+
function saveCanvas (event) {
1010
console.log('clicked!');
1111
}
1212

13-
clearBoard = (event) => {
14-
this.props.clearBoard();
13+
function clearBoard (event) {
14+
props.clearBoard();
1515

16-
if (!this.props.websocket || !this.props.url) return;
16+
if (!props.websocket || !props.url) return;
1717

1818
fetch('http://192.168.0.100:8000/api/board', {
1919
method:'PUT',
@@ -22,14 +22,14 @@ class Toolbar extends React.Component{
2222
'Accept': 'application/json'
2323
},
2424
body: new URLSearchParams({
25-
board_url: this.props.url,
25+
board_url: props.url,
2626
action: "BOARD_CLEAR"
2727
})
2828
})
2929
.then(r => r.json())
3030
.then(response => {
3131
if (response.success){
32-
this.props.websocket.send(JSON.stringify({
32+
props.websocket.send(JSON.stringify({
3333
action: 'clearBoard'
3434
}))
3535
} else {
@@ -38,33 +38,31 @@ class Toolbar extends React.Component{
3838
})
3939
}
4040

41-
render(){
42-
return (
43-
<div className={styles.toolbar}>
44-
<div className={styles.toolbarBlock}>
45-
- Операции -<br/>
46-
<button className={styles.saveButton} id="saveButton" onClick={this.saveCanvas}>
47-
Поделиться
48-
</button>
49-
<button className={styles.clearButton} id="clearButton" onClick={this.clearBoard}>
50-
Очистить
51-
</button>
52-
</div>
53-
<div className={styles.toolbarBlock}>
54-
- Цвет -<br/>
55-
<img onClick={e => this.props.changeBrushColor(actions.TOOLBAR_BRUSH_COLOR_RED)} className={styles.toolbarImgButton} src="http://professorweb.ru/downloads/pen_red.gif" />
56-
<img onClick={e => this.props.changeBrushColor(actions.TOOLBAR_BRUSH_COLOR_GREEN)} className={styles.toolbarImgButton} src="http://professorweb.ru/downloads/pen_green.gif" />
57-
<img onClick={e => this.props.changeBrushColor(actions.TOOLBAR_BRUSH_COLOR_BLUE)} className={styles.toolbarImgButton} src="http://professorweb.ru/downloads/pen_blue.gif" />
58-
</div>
59-
<div className={styles.toolbarBlock}>
60-
- Толщина -<br/>
61-
<img onClick={e => this.props.changeBrushWidth(actions.TOOLBAR_BRUSH_WIDTH_LOW)} className={styles.toolbarImgButton} src="http://professorweb.ru/downloads/pen_thin.gif" />
62-
<img onClick={e => this.props.changeBrushWidth(actions.TOOLBAR_BRUSH_WIDTH_MIDDLE)} className={styles.toolbarImgButton} src="http://professorweb.ru/downloads/pen_medium.gif" />
63-
<img onClick={e => this.props.changeBrushWidth(actions.TOOLBAR_BRUSH_WIDTH_BIG)} className={styles.toolbarImgButton} src="http://professorweb.ru/downloads/pen_thick.gif" />
64-
</div>
41+
return (
42+
<div className={styles.toolbar}>
43+
<div className={styles.toolbarBlock}>
44+
- Операции -<br/>
45+
<button className={styles.saveButton} id="saveButton" onClick={saveCanvas}>
46+
Поделиться
47+
</button>
48+
<button className={styles.clearButton} id="clearButton" onClick={clearBoard}>
49+
Очистить
50+
</button>
6551
</div>
66-
)
67-
}
52+
<div className={styles.toolbarBlock}>
53+
- Цвет -<br/>
54+
<img onClick={e => props.changeBrushColor(actions.TOOLBAR_BRUSH_COLOR_RED)} className={styles.toolbarImgButton} src="http://professorweb.ru/downloads/pen_red.gif" />
55+
<img onClick={e => props.changeBrushColor(actions.TOOLBAR_BRUSH_COLOR_GREEN)} className={styles.toolbarImgButton} src="http://professorweb.ru/downloads/pen_green.gif" />
56+
<img onClick={e => props.changeBrushColor(actions.TOOLBAR_BRUSH_COLOR_BLUE)} className={styles.toolbarImgButton} src="http://professorweb.ru/downloads/pen_blue.gif" />
57+
</div>
58+
<div className={styles.toolbarBlock}>
59+
- Толщина -<br/>
60+
<img onClick={e => props.changeBrushWidth(actions.TOOLBAR_BRUSH_WIDTH_LOW)} className={styles.toolbarImgButton} src="http://professorweb.ru/downloads/pen_thin.gif" />
61+
<img onClick={e => props.changeBrushWidth(actions.TOOLBAR_BRUSH_WIDTH_MIDDLE)} className={styles.toolbarImgButton} src="http://professorweb.ru/downloads/pen_medium.gif" />
62+
<img onClick={e => props.changeBrushWidth(actions.TOOLBAR_BRUSH_WIDTH_BIG)} className={styles.toolbarImgButton} src="http://professorweb.ru/downloads/pen_thick.gif" />
63+
</div>
64+
</div>
65+
)
6866
}
6967

7068
const mapDispatchToProps = {

frontend/pages/b/[board_url].js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export default function SyncBoard() {
3333
}
3434
})
3535

36-
const websocket = new WebSocket("ws://192.168.0.100:8001/board");
36+
const websocket = new WebSocket("ws://192.168.0.100:8001/board/" + board_url);
3737

3838
websocket.onmessage = function (event) {
3939

0 commit comments

Comments
 (0)