扫码订阅《 》或入驻星球,即可阅读文章!

GOLANG ROADMAP

阅读模式

  • 沉浸
  • 自动
  • 日常
首页
Go友会
  • 城市
  • 校园
Go学院
  • Go小课
  • Go小考
  • Go实战
  • 精品课
Go求职
  • 求职辅导🔥
  • Offer收割社群
  • 企业题库
  • 面试宝典
Go宝典
  • 在线宝典
  • B站精选
  • 推荐图书
  • 每日博文
Go仓库
实验区
  • Go周边
  • Go下载
  • Go月刊
消息
更多
  • 用户中心

    • 我的信息
    • 推广返利
  • 玩转星球

    • 星球介绍
    • 角色体系
    • 星主权益
  • 支持与服务

    • 联系星主
    • 成长记录
    • 常见问题
    • 吐槽专区
  • 合作交流

    • 渠道合作
    • 课程入驻
    • 友情链接
author-avatar

GOLANG ROADMAP


首页
Go友会
  • 城市
  • 校园
Go学院
  • Go小课
  • Go小考
  • Go实战
  • 精品课
Go求职
  • 求职辅导🔥
  • Offer收割社群
  • 企业题库
  • 面试宝典
Go宝典
  • 在线宝典
  • B站精选
  • 推荐图书
  • 每日博文
Go仓库
实验区
  • Go周边
  • Go下载
  • Go月刊
消息
更多
  • 用户中心

    • 我的信息
    • 推广返利
  • 玩转星球

    • 星球介绍
    • 角色体系
    • 星主权益
  • 支持与服务

    • 联系星主
    • 成长记录
    • 常见问题
    • 吐槽专区
  • 合作交流

    • 渠道合作
    • 课程入驻
    • 友情链接
  • Iris框架中文文档

    • 概要
    • 功能列表
    • 安装
    • HTTP 主机配置
    • 配置信息
    • HTTP 路由
    • Context机制
    • 动态路由参数
    • 路由命名
    • 路由中间件
    • 打包Router
    • 错误处理
    • MVC 架构
    • MVC电影项目示例
    • MVC之Websockets
    • MVC中使用会话
    • 单一控制器
    • 视图(模板引擎)
    • 会话Sessions
    • Websocket

扫码订阅《 》或入驻星球,即可阅读文章!

MVC之Websockets


GOLANG ROADMAP

# MVC 模式使用 Websockets

package main

import (
    "sync/atomic"

    "github.com/kataras/iris"
    "github.com/kataras/iris/mvc"
    "github.com/kataras/iris/websocket"
)

func main() {
    app := iris.New()
    // 加载模板
    app.RegisterView(iris.HTML("./views", ".html"))

    // 渲染 ./views/index.html.
    app.Get("/", func(ctx iris.Context) {
        ctx.View("index.html")
    })

    mvc.Configure(app.Party("/websocket"), configureMVC)
    // Or mvc.New(app.Party(...)).Configure(configureMVC)

    // http://localhost:8080
    app.Run(iris.Addr(":8080"))
}

func configureMVC(m *mvc.Application) {
    ws := websocket.New(websocket.Config{})
    // http://localhost:8080/websocket/iris-ws.js
    m.Router.Any("/iris-ws.js", websocket.ClientHandler())

    // 将会绑定 ws.Upgrade 的结果 ,它是一个  websocket.Connection
    // 由 `m.Handle` 服务的控制器.
    m.Register(ws.Upgrade)
    m.Handle(new(websocketController))
}

var visits uint64

func increment() uint64 {
    return atomic.AddUint64(&visits, 1)
}

func decrement() uint64 {
    return atomic.AddUint64(&visits, ^uint64(0))
}

type websocketController struct {
    // 注意:你也可以使用匿名字段,因为 binder 将会找到它
    //
    // 这是当前 websocket 的连接,每个客户端都有自己的  *websocketController 实例对象。
    Conn websocket.Connection
}

func (c *websocketController) onLeave(roomName string) {
    // visits--
    newCount := decrement()
    // 这将在所有客户端上调用 「visit」 事件,除了当前的客户端,除了当前的这个,
    // (当前的不能访问时因为,它已经断开了,但是对于任何情况都是使用这种类型的设计)
    c.Conn.To(websocket.Broadcast).Emit("visit", newCount)
}

func (c *websocketController) update() {
    // visits++
    newCount := increment()

    // 这将在所有客户端上调用 「visit」 事件,包括当前的 websocket
    // 使用 'newCount' variable 变量。
		//
    // 你有很多方法可以做得更快,例如你可以发送一个新 vistor
    // 且客户端可以自行增加,但在这里我们只是「展示」websocket 控制器
    c.Conn.To(websocket.All).Emit("visit", newCount)
}

func (c *websocketController) Get( /* websocket.Connection could be lived here as well, it doesn't matter */ ) {
    c.Conn.OnLeave(c.onLeave)
    c.Conn.On("visit", c.update)

    // call it after all event callbacks registration.
    c.Conn.Wait()
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
  • MVC 模式使用 Websockets