websocket 增加多分组 fork https://github.com/olahol/melody
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Ola Holmström 5a3ec43235 golint 9 years ago
examples binary messages and tests 10 years ago
.gitignore binary messages and tests 10 years ago
.travis.yml Issue #3: Add close method 9 years ago
CHANGELOG.md add HandlePong 9 years ago
LICENSE license 10 years ago
README.md add HandlePong 9 years ago
config.go golint 9 years ago
doc.go fix doc.go 10 years ago
envelope.go first version 10 years ago
hub.go ensure order of messages and dispatch error handler when buffer is full 9 years ago
melody.go golint 9 years ago
melody_test.go add HandlePong 9 years ago
session.go golint 9 years ago

README.md

melody

Build Status Coverage Status GoDoc

🎶 Minimalist websocket framework for Go.

Melody is websocket framework based on github.com/gorilla/websocket that abstracts away the tedious parts of handling websockets. It gets out of your way so you can write real-time apps. Features include:

  • Clear and easy interface similar to net/http or Gin.
  • A simple way to broadcast to all or selected connected sessions.
  • Message buffers making concurrent writing safe.
  • Automatic handling of ping/pong and session timeouts.

Install

go get github.com/olahol/melody

Example

Multi channel chat server, error handling left as en exercise for the developer.

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")
}

More examples

Documentation

Contributors

FAQ

If you are getting a 403 when trying to connect to your websocket you can change allow all origin hosts:

m := melody.New()
m.Upgrader.CheckOrigin = func(r *http.Request) bool { return true }