comethandler

package module
v0.0.0-...-2189f8e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 31, 2016 License: MIT Imports: 4 Imported by: 0

README

comethandler

simple demo

main code

the complete demo is in examples/simple

package main

import (
	"net/http"
	"time"

	"github.com/importcjj/comethandler"
)

// New comet handler.
var comet = comethandler.New()

// Broker to receive a message from manager client
// and try it's best to broadcast it to all clients.
func Broker(rw http.ResponseWriter, r *http.Request) {
	message := r.FormValue("message")
	comet.Broadcast([]byte(message))
}

func main() {

	http.Handle("/websocket", comet)
	http.HandleFunc("/broker", Broker)

	// Send a tick to clients every minute.
	go func() {
		for now := range time.Tick(1 * time.Second) {
			comet.Broadcast([]byte(now.Format(time.RFC850)))
		}
	}()

	http.ListenAndServe(":8080", nil)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CometHandler

type CometHandler struct {
	MessageBox chan []byte
	Pool       *ContextPool
}

func New

func New() *CometHandler

func (*CometHandler) Broadcast

func (c *CometHandler) Broadcast(body []byte)

func (*CometHandler) Func

func (c *CometHandler) Func(rw http.ResponseWriter, r *http.Request)

func (*CometHandler) ServeHTTP

func (c *CometHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request)

type Context

type Context struct {
	ReturnSignal   chan bool
	ResponseWriter http.ResponseWriter
	Request        *http.Request
}

Context is the HTTP request keeper.

func NewContext

func NewContext(responseWriter http.ResponseWriter, request *http.Request) *Context

NewContext return a new context.

func (*Context) Wait

func (c *Context) Wait()

Wait to receive a message. then it can be return

func (*Context) Write

func (c *Context) Write(body []byte)

Write to the HTTP request's ResponseWriter and then let's the request return.

type ContextPool

type ContextPool struct {
	List *list.List
	// contains filtered or unexported fields
}

func NewContextPool

func NewContextPool(params ...interface{}) *ContextPool

NewContextPool returns a context pool. size : max size of the poll

func (*ContextPool) Get

func (p *ContextPool) Get() *Context

func (*ContextPool) Len

func (p *ContextPool) Len() int

func (*ContextPool) Put

func (p *ContextPool) Put(context *Context)

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL