func init() {
// Initialize language type list.
langTypes = strings.Split(beego.AppConfig.String("lang_types"), "|")
// Load locale files according to language types.
for _, lang := range langTypes {
beego.Trace("Loading language: " + lang)
if err := i18n.SetMessage(lang, "conf/"+"locale_"+lang+".ini"); err != nil {
beego.Error("Fail to set message file:", err)
return
}
}
}
// baseController represents base router for all other app routers.
//基本控制器为所有其他的app路由提供了基本
// It implemented some methods for the same implementation;
// thus, it will be embedded into other routers.
type baseController struct {
beego.Controller // Embed struct that has stub implementation of the interface.
i18n.Locale // For i18n usage when process data and render template.
}
// Prepare implemented Prepare() method for baseController.
// It's used for language option check and setting.
func (this *baseController) Prepare() {
// Reset language option.
this.Lang = "" // This field is from i18n.Locale.
// 1. Get language information from 'Accept-Language'.
al := this.Ctx.Request.Header.Get("Accept-Language")
if len(al) > 4 {
al = al[:5] // Only compare first 5 letters.
if i18n.IsExist(al) {
this.Lang = al
}
}
// 2. Default language is English.
if len(this.Lang) == 0 {
this.Lang = "en-US"
}
// Set template level language option.
this.Data["Lang"] = this.Lang
}
// AppController handles the welcome screen that allows user to pick a technology and username.
type AppController struct {
baseController // Embed to use methods that are implemented in baseController.
}
// Get implemented Get() method for AppController.
func (this *AppController) Get() {
this.TplName = "welcome.html"
}
//用于解决post请求
func (this *AppController) Join() {
// Get form value.
uname := this.GetString("uname")
tech := this.GetString("tech")
switch tech {
case "longpolling":
this.Redirect("/lp?uname="+uname, 302)
case "websocket":
this.Redirect("/ws?uname="+uname, 302)
default:
this.Redirect("/", 302)
}
// Usually put return after redirect.
return
}
if event.Type == models.EVENT_MESSAGE {
beego.Info("Message from", event.User, ";Content:", event.Content)
}
case unsub := <-unsubscribe:
for sub := subscribers.Front(); sub != nil; sub = sub.Next() {
if sub.Value.(Subscriber).Name == unsub {
subscribers.Remove(sub)
// Clone connection.
ws := sub.Value.(Subscriber).Conn
if ws != nil {
ws.Close()
beego.Error("WebSocket closed:", unsub)
}
publish <- newEvent(models.EVENT_LEAVE, unsub, "") // Publish a LEAVE event.
break
}
}
}
}
}
func init() {
go chatroom()
}
func isUserExist(subscribers *list.List, user string) bool {
for sub := subscribers.Front(); sub != nil; sub = sub.Next() {
if sub.Value.(Subscriber).Name == user {
return true
}
}
return false
}
longpolling.go:长轮询模式的控制器和方法
package controllers
import (
"samples/WebIM/models"
)
// LongPollingController handles long polling requests.
type LongPollingController struct {
baseController
}
// Join method handles GET requests for LongPollingController.
func (this *LongPollingController) Join() {
// Safe check.
uname := this.GetString("uname")
if len(uname) == 0 {
this.Redirect("/", 302)
return
}