sago

package module
v0.0.0-...-df73fed Latest Latest
Warning

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

Go to latest
Published: Aug 29, 2013 License: MIT Imports: 4 Imported by: 1

README

Sago (Socket Action Go)

Sago is a framework for web development with the use of websockets. Gives the use of simple actions to pass between server and client for real-time communication.

Install

$ go get github.com/craigjackson/sago

Usage

Will need to import sago.

import (
  "github.com/craigjackson/sago"
)

Need to add the actions at the first part of main() then use Run() to start the server:

func main() {
  sago.AddAction("ping", func(r *sago.Request) {
    r.Session.Send(r.Id, "pong", r.Args)
  })
  sago.Run()
}

On "ping" from the client, we will send a reply (passing the Id in the first parameter) JSON message back to the client with the action "pong". The extra Args passed back will just be the exact data the client passed to us.

Now, you just need to start the server from the command-line:

$ go run app.go

On the client-side now, you can just use the following in javascript to see sago in action:

var ws = new WebSocket("ws://localhost:4000/ws");
ws.onmessage = function(message) { console.log(message) };
ws.send(JSON.stringify({ id: "sago123", action: "ping", data: { foo: "bar" } }));

Settings

Two methods for overriding default settings.

1.

For server settings, you may use in environment variables.

SERVER_HOST=example.com SERVER_PORT=80 go run app.go
2.

For all settings, you may import settings and set the variable yourself. Make sure to establish as early in the process as possible (especially before sago.Run()).

import "github.com/craigjackson/sago/settings"

//...

func main() {
  settings.SERVER_HOST = "localhost"
  settings.SERVER_PORT = "4001"
  settings.WEBSOCKET_PATH = "/ws_now_here"
  //...
  sago.Run()
}

Thanks

Thanks to @braindev for feedback and ideas on this project.

License

The MIT License - Copyright (c) 2013 Craig Jackson

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddAction

func AddAction(name string, handler func(*Request))

func Run

func Run()

func SendAll

func SendAll(name string, args map[string]interface{})

func SendAllExcept

func SendAllExcept(name string, args map[string]interface{}, exceptions []*Session)

Types

type Request

type Request struct {
	Session *Session
	Id      string
	Args    *map[string]interface{}
}

type Session

type Session struct {
	Data map[string]interface{}
	// contains filtered or unexported fields
}

func (Session) Send

func (s Session) Send(response_id string, name string, args map[string]interface{}) error

func (Session) Start

func (s Session) Start()

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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