broacast exit

This commit is contained in:
AJ ONeal 2018-08-02 02:25:10 -06:00
parent 44d605d38a
commit 2b88da7a71
3 changed files with 22 additions and 27 deletions

View File

@ -220,7 +220,7 @@ func postMsg(req *restful.Request, resp *restful.Response) {
return
}
msg := myMsg{}
msg := chatMsg{}
err := req.ReadEntity(&msg)
if nil != err {
fmt.Fprintf(resp, "{ \"error\": { \"code\": \"E_FORMAT\", \"message\": \"invalid json POST\"} }")

View File

@ -33,6 +33,14 @@ func handleTelnetConn(bufConn bufferedConn) {
if nil != err {
if io.EOF != err {
fmt.Fprintf(os.Stderr, "Non-EOF socket error: %s\n", err)
} else {
broadcastMsg <- chatMsg{
sender: nil,
Message: fmt.Sprintf("<%s> left #general\r\n", u.email),
ReceivedAt: time.Now(),
Channel: "general",
User: "system",
}
}
if nil != u {
@ -166,7 +174,7 @@ func handleTelnetConn(bufConn bufferedConn) {
continue
}
broadcastMsg <- myMsg{
broadcastMsg <- chatMsg{
ReceivedAt: time.Now(),
sender: bufConn,
Message: strings.TrimRight(msg, "\r\n"),

View File

@ -73,9 +73,7 @@ func (b bufferedConn) Read(p []byte) (int, error) {
return b.r.Read(p)
}
// Just making these all globals right now
// because... I can clean it up later
type myMsg struct {
type chatMsg struct {
sender net.Conn
Message string `json:"message"`
ReceivedAt time.Time `json:"received_at"`
@ -83,17 +81,17 @@ type myMsg struct {
User string `json:"user"`
}
type JsonMsg struct {
Messages []*myMsg `json:"messages"`
Messages []*chatMsg `json:"messages"`
}
type chatHist struct {
msgs []*myMsg
msgs []*chatMsg
i int
c int
}
var myChatHist chatHist
var broadcastMsg chan myMsg
var broadcastMsg chan chatMsg
var virginConns chan net.Conn
var wantsServerHello chan bufferedConn
@ -318,16 +316,16 @@ func main() {
demuxHttpClient = make(chan bufferedConn, 128)
// cruft to delete
//myRooms = make(map[string](chan myMsg))
//myRooms = make(map[string](chan chatMsg))
//myRooms["general"] = make(chan myMsg, 128)
//myRooms["general"] = make(chan chatMsg, 128)
// Note: I had considered dynamically select on channels for rooms.
// https://stackoverflow.com/questions/19992334/how-to-listen-to-n-channels-dynamic-select-statement
// I don't think that's actually the best approach, but I just wanted to save the link
broadcastMsg = make(chan myMsg, 128)
broadcastMsg = make(chan chatMsg, 128)
// Poor-Man's container/ring (circular buffer)
myChatHist.msgs = make([]*myMsg, 128)
myChatHist.msgs = make([]*chatMsg, 128)
var addr string
if 0 != int(*port) {
@ -400,10 +398,9 @@ func main() {
telnetConns[u.bufConn] = u
// is chan chan the right way to handle this?
u.userCount <- len(telnetConns)
broadcastMsg <- myMsg{
sender: nil,
// TODO fmt.Fprintf()? template?
Message: "<" + u.email + "> joined #general\n",
broadcastMsg <- chatMsg{
sender: nil,
Message: fmt.Sprintf("<%s> joined #general\r\n", u.email),
ReceivedAt: time.Now(),
Channel: "general",
User: "system",
@ -428,18 +425,8 @@ func main() {
case bufConn := <-wantsServerHello:
go handleTelnetConn(bufConn)
case u := <-cleanTelnet:
// we can safely ignore this error, if any
if "" != u.email {
broadcastMsg <- myMsg{
sender: nil,
// TODO fmt.Fprintf()? template?
Message: "<" + u.email + "> left #general\n",
ReceivedAt: time.Now(),
Channel: "general",
User: "system",
}
}
close(u.newMsg)
// we can safely ignore this error, if any
u.bufConn.Close()
delete(telnetConns, u.bufConn)
case bufConn := <-gotClientHello: