我有一个Flask_SocketIO应用,该应用应该实现聊天组系统。 与之交互的客户端是一个颤抖的应用程序。 我编写了一个测试,以查看socketio事件是否正常运行。它曾经工作过,但没有停止过。 服务器正在获取客户端的发射,但没有返回给客户端。 同样,与连接有关的evet(连接,断开连接,错误)似乎也可以正常工作。客户在这些事件上的回调被调用。
我的flutter测试客户端:
void main() async {
setupLocator();
final api = locator<Api>();
final socketService = locator<SocketService>();
Message msg = Message(
msgId: null,
content: "Hello!",
senderId: 1,
senderName: 'tair',
sendtime: DateTime.now().toString(),
groupId: 1);
runApp(
MaterialApp(
home: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
RaisedButton(
child: Text("Disconnect"),
onPressed: () {
socketService.leave(1, 1);
}),
RaisedButton(
child: Text("Send"),
onPressed: () {
socketService.sendMessage(msg);
},
),
RaisedButton(
child: Text("Connect"),
onPressed: () {
socketService.connect(1, 1, (data) {
print('Data!');
print(data);
});
},
),
],
),
),
),
);
SharedPreferences.setMockInitialValues({});
await api.login('tair', '1234');
await socketService.getToken();
socketService.connect(1, 1, (data) {
print('Data!');
print(data);
});
connect()
方法
参数以加入服务器端的套接字房间get_it
进行依赖注入。也不相关这是我服务器上的事件:
@sock.on('join')
def join_group_room(data):
print(data)
token = data['token']
if token in user_token.keys():
group = Group.query.filter_by(id=int(data['groupId'])).first()
if group.temp_participants is None:
group.temp_participants = data['userId'] + ','
else:
group.temp_participants += data['userId'] + ','
db.session.commit()
join_room(data['groupId'])
#print(rooms())
else:
emit('error', 'Invalid token')
@sock.on('message')
def message_room(data):
print(data)
token = data['token']
if token in user_token.keys():
message = Message(content=data['message'], groupid=int(data['groupId']), username=user_token[token],
datetime=data['datetime'])
db.session.add(message)
db.session.commit()
participants = Group.query.filter_by(id=message.groupid).first().participants.split(",")
temp_participants = Group.query.filter_by(id=message.groupid).first().temp_participants.split(",")
for participant in participants:
if participant not in temp_participants:
pushbots.push_batch(platform=pushbots.PLATFORM_ANDROID,
alias=participant,
msg='A new message arrived', payload={'data': {'message': message.content,
'messageId': message.id,
'username': user_token[
token],
'datetime': message.datetime,
'groupId': message.groupid,
'userId': User.query.filter_by(
username=user_token[
token]).first().id}})
print("Emitting")
emit('message', {'message': message.content, 'messageId': message.id,
'username': user_token[token], 'datetime': message.datetime,
'groupId': message.groupid,
'userId': User.query.filter_by(username=user_token[token]).first().id},
room=message.groupid)
sock.sleep(0)
else:
emit('error', 'Invalid token')
@sock.on('leave')
def leave_group_room(data):
print(data)
token = data['token']
if token in user_token.keys():
group = Group.query.filter_by(id=int(data['groupId'])).first()
group.temp_participants = str(group.temp_participants.split(",").remove(data['userId'])).strip('[]')
db.session.commit()
leave_room(data['groupId'])
emit('error', 'Invalid token')
我将eventlet用作socketio应用程序的async_mode。我在网上查找解决方案,很多人说我应该在主脚本中添加以下行:
import eventlet
eventlet.monkey_patch()
此外,据我在该项目上的合作伙伴称,这些事件在他的计算机上运行正常
进一步的解释,这是我的git repo的链接,因此您可以观看整个代码:My git repo(位于Integration / ClientServer分支上)
感谢您的帮助!
答案 0 :(得分:0)
很显然,房间名称只能是字符串,但是如果您将int传递为房间参数,那么函数emit不会抛出错误。