PHP中高级工程师面试重点讲解视频课程
Go快速入门浅显易懂视频教程-基础篇
Go快速入门浅显易懂视频教程-中级篇
beego聊天室的基本配置
阅读:1700 分享次数:0

 配置文件app.conf修改为
$ autorender = false

controller/default.go默认为

    package controllers
    
    import (
        "github.com/astaxie/beego"
    )
    
    type MainController struct {
        beego.Controller
    }
    
    func (this *MainController) Get() {
        this.Render()
    }

在routers/router.go添加socket连接的路由

    beego.Router("/ws", &controllers.WsController{})
    beego.Router("/join", &controllers.WsController{},"get:Join")

controller/ws.go文件内容

    package controllers
    
    import (
        "github.com/astaxie/beego"
    )
    
    type WsController struct {
        beego.Controller
    }
    
    func (this *WsController) Get() {
        this.Render()
    }
    
    func (this *WsController) Join() {
        username := this.GetString("username")
        room := this.GetString("room")
        if (len(username)) == 0 {
            this.Redirect("/", 302)
            return
        }
        if (len(room)) == 0 {
            this.Redirect("/", 302)
            return
        }
        this.Render()
    }

view默认生成的路径是maincontroller/get.tpl
get.tpl的默认文件是如下:
    
    <!DOCTYPE html>
    
    <html>
    <head>
      <title>测试聊天室-首页</title>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    </head>
    
    <body>
        <form method="get" action="/join">
            <input name="username" />
            <input name="room" />
            <button type="submmit">进入聊天室</button>
        </form>
    </body>
    </html>

感觉本站内容不错,读后有收获?