Skip to content

Commit

Permalink
客户端消息包容错
Browse files Browse the repository at this point in the history
  • Loading branch information
kooritea committed Apr 7, 2020
1 parent dd0c9d8 commit a3f185a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
18 changes: 9 additions & 9 deletions src/model/ClientSocketPacket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ export class AuthClientSocketPacket extends ClientSocketPacket {
constructor(packet: ClientSocketPacket) {
super(packet)
this.data = {
token: String(packet.data.token || ""),
name: String(packet.data.name || ""),
group: String(packet.data.group || "")
token: String(packet?.data?.token || ""),
name: String(packet?.data?.name || ""),
group: String(packet?.data?.group || "")
}
if (!this.data.name) {
throw new Error("name property is required")
Expand All @@ -61,15 +61,15 @@ export class MessageClientSocketPacket extends ClientSocketPacket {
sendType: packet.data.sendType === 'personal' ? 'personal' : 'group',
target: packet.data.target,
message: {
text: String(packet.data.message.text || ""),
desp: String(packet.data.message.desp || ""),
extra: packet.data.message.extra || {}
text: String(packet?.data?.message?.text || ""),
desp: String(packet?.data?.message?.desp || ""),
extra: typeof packet?.data?.message?.extra === 'object' ? packet.data.message.extra : {}
}
}
if (!this.data.target) {
throw new Error("target property is required")
}
if (packet.data.sendType !== 'personal' && packet.data.sendType !== 'group') {
if (packet?.data?.sendType !== 'personal' && packet.data.sendType !== 'group') {
throw new Error("sendType property must 'personal' or 'group'")
}
}
Expand All @@ -81,9 +81,9 @@ export class MgsCbClientSocketPacket extends ClientSocketPacket {
constructor(packet: ClientSocketPacket) {
super(packet)
this.data = {
mid: String(packet.data.mid || "")
mid: String(packet?.data?.mid || "")
}
if (!this.data.mid) {
if (!this?.data?.mid) {
throw new Error("mid property is required")
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/servers/Http.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export class HttpServer {
target: packet.data.target,
from: {
method: 'http',
name: '',
name: clientSocketPacket?.auth?.name || '',
},
message: packet.data.message
})
Expand Down

0 comments on commit a3f185a

Please sign in to comment.