mirror of
https://github.com/astaxie/beego.git
synced 2024-11-23 08:40:55 +00:00
43 lines
782 B
Go
43 lines
782 B
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"github.com/astaxie/beego"
|
||
|
"github.com/fzzy/sockjs-go/sockjs"
|
||
|
"strings"
|
||
|
)
|
||
|
|
||
|
var users *sockjs.SessionPool = sockjs.NewSessionPool()
|
||
|
|
||
|
func chatHandler(s sockjs.Session) {
|
||
|
users.Add(s)
|
||
|
defer users.Remove(s)
|
||
|
|
||
|
for {
|
||
|
m := s.Receive()
|
||
|
if m == nil {
|
||
|
break
|
||
|
}
|
||
|
fullAddr := s.Info().RemoteAddr
|
||
|
addr := fullAddr[:strings.LastIndex(fullAddr, ":")]
|
||
|
m = []byte(fmt.Sprintf("%s: %s", addr, m))
|
||
|
users.Broadcast(m)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
type MainController struct {
|
||
|
beego.Controller
|
||
|
}
|
||
|
|
||
|
func (m *MainController) Get() {
|
||
|
m.TplNames = "index.html"
|
||
|
}
|
||
|
|
||
|
func main() {
|
||
|
conf := sockjs.NewConfig()
|
||
|
sockjshandler := sockjs.NewHandler("/chat", chatHandler, conf)
|
||
|
beego.Router("/", &MainController{})
|
||
|
beego.RouterHandler("/chat/:info(.*)", sockjshandler)
|
||
|
beego.Run()
|
||
|
}
|