-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
196 lines (171 loc) · 6.31 KB
/
app.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import os
from flask import Flask, render_template, request, redirect, url_for, flash, session
from flask_socketio import SocketIO, emit, join_room, leave_room
from flask_session import Session
import json
from classes import (
Channel,
Message,
User
)
app = Flask(__name__)
app.config["SECRET_KEY"] = "1234567890"
app.config["SESSION_TYPE"] = "filesystem"
SESSION_TYPE = 'filesystem'
socketio = SocketIO(app)
Session(app)
# 当前频道列表
channels = []
# 当前用户列表
users = []
channels.append(Channel(index=0, name="第一组"))
channels.append(Channel(index=1, name="第二组"))
channels.append(Channel(index=2, name="第三组"))
channels.append(Channel(index=3, name="第四组"))
channels.append(Channel(index=4, name="第五组"))
channels.append(Channel(index=5, name="第六组"))
channels.append(Channel(index=6, name="第七组"))
channels.append(Channel(index=7, name="第八组"))
channels.append(Channel(index=8, name="第九组"))
channels.append(Channel(index=9, name="第十组"))
@app.route("/", methods=["GET", "POST"])
def index():
# 显示index页面
if request.method == "GET":
return render_template("index.html")
# 处理登入信息
else:
# 创建一个user类
u_name = request.form.get("nickname")
print("登入请求:", u_name)
new_user = User(u_name, 0)
# 如果当前用户列表中存在该用户,则返回原处,并提示
if request.form.get("nickname") in [u.name for u in users]:
flash("该名称已被使用!")
return redirect("/", "303")
# 设置当前会话的用户信息,返回频道列表
session["user"] = u_name
session["channel"] = 0
users.append(new_user)
print('登入!当前用户:', users)
return redirect("/channels")
@app.route("/channels", methods={"GET"})
def channels_view():
# 已经注册过
if session.get("user"):
u_name = session["user"]
# 用户点击特定频道,将该频道与用户绑定
if request.args.get("ch"):
new_channel = int(request.args.get("ch"))
session["channel"] = new_channel
return redirect("/channel")
else:
return render_template("channels.html", channels=channels, user=u_name)
# 若尚未注册,返回、报错
else:
flash("尚未登陆!")
return redirect("/", "303")
@app.route("/channel")
def channel_view():
# 已经注册过
if session.get("user"):
u_name = session["user"]
# 此时接收到由/channels路由处绑定的用户频道
u_channel = session["channel"]
# 返回改频道的所有消息及用户
return render_template("channel.html", channel=channels[u_channel], user=u_name, prev_user="None")
# 若尚未注册,返回、报错
else:
flash("尚未登陆!")
return redirect("/", "303")
@app.route("/create_channel", methods=["GET", "POST"])
def create_channel():
if request.method == "GET":
return render_template("create_channel.html")
else:
# 已经注册过
if session.get("user"):
# POST方法提交建立频道名称
new_channel_name = request.form.get("channel_name")
# index递增
new_channel = Channel(index=len(channels), name=new_channel_name)
# 如果当前频道列表中存在该频道,则返回原处,并提示
if new_channel.name in [c.name for c in channels]:
flash("该名称已被使用!")
return redirect("/create_channel", "303")
channels.append(new_channel)
flash("频道创建成功!")
return redirect("/channels")
# 若尚未注册,返回、报错
else:
flash("尚未注册!")
return redirect("/", "303")
@app.route("/logout")
def logout():
try:
del_user = User(session['user'], session["channel"])
del_from = Channel(session["channel"], 0)
except KeyError:
return redirect("/", "303")
try:
users.remove(del_user)
channels[channels.index(del_from)].users.remove(session['user'])
except ValueError:
pass
session.clear()
print('登出!当前用户:', users)
return redirect("/")
@socketio.on("register_on_channel")
def select_channel(data):
u_name = data["nickname"]
u_channel = int(data["channel"])
u_name = u_name.encode("raw_unicode_escape").decode("utf-8")
print(u_name, u_channel)
join_room(u_channel)
# 防止重复添加
if u_name not in channels[u_channel].users:
channels[u_channel].users.append(u_name)
# 更新并广播
nickname_array = json.dumps(channels[u_channel].users)
print(nickname_array)
emit("current_client_list", {"users": nickname_array}, room=u_channel)
@socketio.on("new_message")
def new_message(data):
u_name = data["nickname"]
u_channel = int(data["channel"])
u_name = u_name.encode("raw_unicode_escape").decode("utf-8")
user = User(u_name, u_channel)
print(u_name, u_channel)
if user in users:
join_room(u_channel)
text = data["message"]
text = text.encode("raw_unicode_escape").decode("utf-8")
msg = Message(user, text)
channels[u_channel].add_message(msg)
print("收到消息:", text, "from", u_name)
emit("write_message", {"nickname": u_name, "message": text}, room=u_channel)
nickname_array = json.dumps(channels[session["channel"]].users)
emit("current_client_list", {"users": nickname_array}, room=session["channel"])
else:
print('未登录!')
@socketio.on('connect')
def connect():
print("上线", request.args.get("t"))
@socketio.on('disconnect')
def disconnect():
pass
try:
del_user = User(session['user'], session["channel"])
del_from = Channel(session["channel"], 0)
try:
channels[channels.index(del_from)].users.remove(session['user'])
except ValueError:
pass
nickname_array = json.dumps(channels[session["channel"]].users)
print(nickname_array)
emit("current_client_list", {"users": nickname_array}, room=session["channel"])
print('掉线: ', session['user'])
except KeyError:
pass
if __name__ == '__main__':
app.run()