broacast exit
This commit is contained in:
parent
44d605d38a
commit
2b88da7a71
|
@ -220,7 +220,7 @@ func postMsg(req *restful.Request, resp *restful.Response) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
msg := myMsg{}
|
msg := chatMsg{}
|
||||||
err := req.ReadEntity(&msg)
|
err := req.ReadEntity(&msg)
|
||||||
if nil != err {
|
if nil != err {
|
||||||
fmt.Fprintf(resp, "{ \"error\": { \"code\": \"E_FORMAT\", \"message\": \"invalid json POST\"} }")
|
fmt.Fprintf(resp, "{ \"error\": { \"code\": \"E_FORMAT\", \"message\": \"invalid json POST\"} }")
|
||||||
|
|
|
@ -33,6 +33,14 @@ func handleTelnetConn(bufConn bufferedConn) {
|
||||||
if nil != err {
|
if nil != err {
|
||||||
if io.EOF != err {
|
if io.EOF != err {
|
||||||
fmt.Fprintf(os.Stderr, "Non-EOF socket error: %s\n", 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 {
|
if nil != u {
|
||||||
|
@ -166,7 +174,7 @@ func handleTelnetConn(bufConn bufferedConn) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
broadcastMsg <- myMsg{
|
broadcastMsg <- chatMsg{
|
||||||
ReceivedAt: time.Now(),
|
ReceivedAt: time.Now(),
|
||||||
sender: bufConn,
|
sender: bufConn,
|
||||||
Message: strings.TrimRight(msg, "\r\n"),
|
Message: strings.TrimRight(msg, "\r\n"),
|
||||||
|
|
|
@ -73,9 +73,7 @@ func (b bufferedConn) Read(p []byte) (int, error) {
|
||||||
return b.r.Read(p)
|
return b.r.Read(p)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Just making these all globals right now
|
type chatMsg struct {
|
||||||
// because... I can clean it up later
|
|
||||||
type myMsg struct {
|
|
||||||
sender net.Conn
|
sender net.Conn
|
||||||
Message string `json:"message"`
|
Message string `json:"message"`
|
||||||
ReceivedAt time.Time `json:"received_at"`
|
ReceivedAt time.Time `json:"received_at"`
|
||||||
|
@ -83,17 +81,17 @@ type myMsg struct {
|
||||||
User string `json:"user"`
|
User string `json:"user"`
|
||||||
}
|
}
|
||||||
type JsonMsg struct {
|
type JsonMsg struct {
|
||||||
Messages []*myMsg `json:"messages"`
|
Messages []*chatMsg `json:"messages"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type chatHist struct {
|
type chatHist struct {
|
||||||
msgs []*myMsg
|
msgs []*chatMsg
|
||||||
i int
|
i int
|
||||||
c int
|
c int
|
||||||
}
|
}
|
||||||
|
|
||||||
var myChatHist chatHist
|
var myChatHist chatHist
|
||||||
var broadcastMsg chan myMsg
|
var broadcastMsg chan chatMsg
|
||||||
|
|
||||||
var virginConns chan net.Conn
|
var virginConns chan net.Conn
|
||||||
var wantsServerHello chan bufferedConn
|
var wantsServerHello chan bufferedConn
|
||||||
|
@ -318,16 +316,16 @@ func main() {
|
||||||
demuxHttpClient = make(chan bufferedConn, 128)
|
demuxHttpClient = make(chan bufferedConn, 128)
|
||||||
|
|
||||||
// cruft to delete
|
// 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.
|
// Note: I had considered dynamically select on channels for rooms.
|
||||||
// https://stackoverflow.com/questions/19992334/how-to-listen-to-n-channels-dynamic-select-statement
|
// 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
|
// 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)
|
// Poor-Man's container/ring (circular buffer)
|
||||||
myChatHist.msgs = make([]*myMsg, 128)
|
myChatHist.msgs = make([]*chatMsg, 128)
|
||||||
|
|
||||||
var addr string
|
var addr string
|
||||||
if 0 != int(*port) {
|
if 0 != int(*port) {
|
||||||
|
@ -400,10 +398,9 @@ func main() {
|
||||||
telnetConns[u.bufConn] = u
|
telnetConns[u.bufConn] = u
|
||||||
// is chan chan the right way to handle this?
|
// is chan chan the right way to handle this?
|
||||||
u.userCount <- len(telnetConns)
|
u.userCount <- len(telnetConns)
|
||||||
broadcastMsg <- myMsg{
|
broadcastMsg <- chatMsg{
|
||||||
sender: nil,
|
sender: nil,
|
||||||
// TODO fmt.Fprintf()? template?
|
Message: fmt.Sprintf("<%s> joined #general\r\n", u.email),
|
||||||
Message: "<" + u.email + "> joined #general\n",
|
|
||||||
ReceivedAt: time.Now(),
|
ReceivedAt: time.Now(),
|
||||||
Channel: "general",
|
Channel: "general",
|
||||||
User: "system",
|
User: "system",
|
||||||
|
@ -428,18 +425,8 @@ func main() {
|
||||||
case bufConn := <-wantsServerHello:
|
case bufConn := <-wantsServerHello:
|
||||||
go handleTelnetConn(bufConn)
|
go handleTelnetConn(bufConn)
|
||||||
case u := <-cleanTelnet:
|
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)
|
close(u.newMsg)
|
||||||
|
// we can safely ignore this error, if any
|
||||||
u.bufConn.Close()
|
u.bufConn.Close()
|
||||||
delete(telnetConns, u.bufConn)
|
delete(telnetConns, u.bufConn)
|
||||||
case bufConn := <-gotClientHello:
|
case bufConn := <-gotClientHello:
|
||||||
|
|
Loading…
Reference in New Issue