Merge USER command into PULL command

This commit is contained in:
cyp0633 2022-11-27 00:26:12 +08:00
parent a44b25e3e7
commit ebf641a8d2
2 changed files with 18 additions and 5 deletions

View File

@ -12,7 +12,7 @@ The server part of Computer Networking course project, a chat software based on
要发送消息,可以使用 `SEND <dest_ip> MSG <message>`。正常情况下返回 `OK`
要获取用户列表,可以使用 `USER` 命令。正常情况下返回:
<DEPRECATED>获取用户列表,可以使用 `USER` 命令。正常情况下返回:
```plain
<user_num> USERS
@ -21,14 +21,20 @@ The server part of Computer Networking course project, a chat software based on
...
```
要接收消息,可以使用 `PULL`。正常情况下返回:
要接收消息和用户列表,可以使用 `PULL`。正常情况下返回:
```plain
LEN <length>
<msg_num> MESSAGES
FROM <from_ip1> CONTENT <content1>
FROM <from_ip2> CONTENT <content2>
FROM <from_ip2> CONTENT <content2>
...
END PULL
<user_num> USERS
<ip1>
<ip2>
...
END USER
```
此外,还可以通过 UDP 广播获得服务端地址。服务器监听 65433/udp 端口,你可以发送 `PROBE`,服务器会返回 `HERE` 到 65433/udp 端口。

View File

@ -53,12 +53,19 @@ func ProcessSend(msg string, from string) (reply string) {
var Pull = regexp.MustCompile(`^PULL`)
func ProcessPull(client Client) (reply string) {
reply = "LEN " + fmt.Sprint(len(client.Messages)) + "\n"
// 返回消息信息
reply = fmt.Sprintf("%v MESSAGES\n", len(client.Messages))
for len(client.Messages) > 0 {
msg := <-client.Messages
reply += "FROM " + msg.From + " CONTENT " + msg.Content + "\n"
}
reply += "END\n"
reply += "END PULL\n"
// 返回用户信息
reply += fmt.Sprintf("%v USERS\n", len(Clients))
for _, u := range Clients {
reply += u.Addr + "\n"
}
reply += "END USER\n"
Logger.Info("Pull", zap.String("client", client.Addr), zap.String("reply", reply))
return
}