websocketjsonrpc

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

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

Go to latest
Published: Apr 18, 2015 License: MIT Imports: 2 Imported by: 0

README

websocketjsonrpc

JSON-RPC over websocket

Example usage

package main

import (
    "log"
    "net/http"
    "net/rpc"
    "net/rpc/jsonrpc"

    "github.com/gorilla/websocket"
    "github.com/s111/websocketjsonrpc"
)

var upgrader = websocket.Upgrader{
    CheckOrigin: func(r *http.Request) bool {
        return true
    },
}

type Args struct {
    A int
    B int
}

type Arith int

func (t *Arith) Multiply(args *Args, reply *int) error {
    *reply = args.A * args.B

    return nil
}

func main() {
    rpc.Register(new(Arith))

    http.HandleFunc("/ws", serveWs)

    err := http.ListenAndServe(":3001", nil)

    if err != nil {
        log.Fatal("ListenAndServe: ", err)
    }
}

func serveWs(w http.ResponseWriter, r *http.Request) {
    if r.Method != "GET" {
        http.Error(w, "Method not allowed", 405)

        return
    }

    ws, err := upgrader.Upgrade(w, r, nil)

    if err != nil {
        log.Println("Upgrade:", err)

        return
    }

    jsonrpc.ServeConn(websocketjsonrpc.NewConn(ws))
}

Documentation

Overview

Package websocketjsonrpc takes a *github.com/gorilla/websocket.Conn and wraps it with the io.ReaderWriterCloser interface.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewConn

func NewConn(ws *websocket.Conn) *conn

NewConn returns a new connection implementing the io.ReadWriteCloser interface

Types

This section is empty.

Jump to

Keyboard shortcuts

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