Go 的 WebSocket 框架 Melody

码农软件 · 软件分类 · WebSocket开发包 · 2019-04-17 16:13:55

软件介绍

Melody 是一个 Go 语言的微型 WebSocket 框架,基于 github.com/gorilla/websocket 开发,主要特性:

  • 接口简单易用,类似 net/http 或者 Gin

  • 提供给所有广播以及给选择连接会话广播的简单途径

  • 消息缓冲对并发写是安全的

  • 可自动处理 ping/pong 和会话超时

一个简单的实例:

Chat demo

代码:

package main

import (
    "github.com/olahol/melody"
    "github.com/gin-gonic/gin"
    "net/http"
)

func main() {
    r := gin.Default()
    m := melody.New()

    r.GET("/", func(c *gin.Context) {
        http.ServeFile(c.Writer, c.Request, "index.html")
    })

    r.GET("/channel/:name", func(c *gin.Context) {
        http.ServeFile(c.Writer, c.Request, "chan.html")
    })

    r.GET("/channel/:name/ws", func(c *gin.Context) {
        m.HandleRequest(c.Writer, c.Request)
    })

    m.HandleMessage(func(s *melody.Session, msg []byte) {
        m.BroadcastFilter(msg, func(q *melody.Session) bool {
            return q.Request.URL.Path == s.Request.URL.Path
        })
    })

    r.Run(":5000")
}

本文地址:https://www.codercto.com/soft/d/3789.html

Elements of Information Theory

Elements of Information Theory

Thomas M. Cover、Joy A. Thomas / Wiley-Blackwell / 2006-7 / GBP 76.50

The latest edition of this classic is updated with new problem sets and material The Second Edition of this fundamental textbook maintains the book's tradition of clear, thought-provoking instr......一起来看看 《Elements of Information Theory》 这本书的介绍吧!

随机密码生成器
随机密码生成器

多种字符组合密码

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具