Send message
This commit is contained in:
parent
02b222e085
commit
8912acef5a
|
@ -0,0 +1,8 @@
|
|||
package internal
|
||||
|
||||
type Message struct {
|
||||
From string
|
||||
To string
|
||||
Time string
|
||||
Content string
|
||||
}
|
|
@ -1,8 +1,12 @@
|
|||
package internal
|
||||
|
||||
import "regexp"
|
||||
import (
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var Helo = regexp.MustCompile(`^HELO .+`)
|
||||
// HELO 信息格式,如:HELO
|
||||
var Helo = regexp.MustCompile(`^HELO$`)
|
||||
|
||||
// ProcessHelo 返回在线客户端列表
|
||||
func ProcessHelo(msg string) (clients string) {
|
||||
|
@ -12,3 +16,24 @@ func ProcessHelo(msg string) (clients string) {
|
|||
}
|
||||
return
|
||||
}
|
||||
|
||||
// SEND 信息格式,如:SEND 127.0.0.1 MSG This is Message
|
||||
var Send = regexp.MustCompile(`/^SEND [0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3} MSG .+`)
|
||||
|
||||
// 提取消息使用
|
||||
var sendMessage = regexp.MustCompile(`MSG .+`)
|
||||
|
||||
// ProcessSend 发送消息
|
||||
func ProcessSend(msg string, from string) (reply string) {
|
||||
temp := strings.Split(msg, "")
|
||||
addr := temp[1]
|
||||
content := sendMessage.FindString(msg)[4:]
|
||||
message := Message{From: from, To: addr, Content: content}
|
||||
for _, client := range Clients {
|
||||
if client.Addr == addr {
|
||||
client.Messages <- message
|
||||
reply = "OK"
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
|
|
@ -25,7 +25,7 @@ func TCPHandler(conn net.Conn) {
|
|||
case Helo.MatchString(msg):
|
||||
reply = ProcessHelo(msg)
|
||||
case Send.MatchString(msg):
|
||||
reply = ProcessSend(msg)
|
||||
reply = ProcessSend(msg, client.Addr)
|
||||
}
|
||||
_, err = conn.Write([]byte(reply))
|
||||
if err != nil {
|
||||
|
|
Loading…
Reference in New Issue