gowebsocket

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

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

Go to latest
Published: Nov 6, 2019 License: MIT Imports: 6 Imported by: 0

README

gowebsocket

Websocket server wrapper
Getting started
Requirements
  • Go 1.7+ earlier versions untested
Instructions
  1. Get deps:
go get github.com/tectiv3/gowebsocket
  1. Example:
package main

import (
	ws "github.com/tectiv3/gowebsocket"
	"log"
	"net/http"
	"os"
)

var (
	wsCh          ws.MessagesChannel
	wsServer      *ws.Server
)

func main() {
	http.Handle("/", http.FileServer(http.Dir("public")))

	wsServer = ws.NewWebsocket("/websocket")

	go func() {
		for {
			select {
			case incoming := <-wsServer.Messages:
				go answerMessage(incoming.Client, incoming.Msg)
			}
		}
	}()

	go wsServer.Listen()

	log.Println("Running webserver on port 8787")
	if err := http.ListenAndServe(":8787", nil); err != nil {
		Log.Error(err)
	}
}

func answerMessage(client *ws.Client, msg *ws.Message) {
	switch msg.Type {
	case "connect":
		client.Send(&ws.Message{Type: "success", Text: "connected"})
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

func NewClient

func NewClient(conn Connection, server *Server) *Client

func (*Client) Done

func (c *Client) Done()

func (*Client) Listen

func (c *Client) Listen()

Listen Write and Read request via chanel

func (*Client) Send

func (c *Client) Send(msg *Message)

type ClientMessage

type ClientMessage struct {
	Client *Client
	Msg    *Message
}

type Connection

type Connection interface {
	ReadMessage() (Message, error)
	WriteMessage(*Message) error
}

type Message

type Message struct {
	ID     int                    `json:"id,omitempty"`
	Text   string                 `json:"text,omitempty"`
	Type   string                 `json:"type"`
	Result interface{}            `json:"result,omitempty"`
	Params map[string]interface{} `json:"params,omitempty"`
}

type MessagesChannel

type MessagesChannel <-chan *ClientMessage

type Server

type Server struct {
	Messages chan *ClientMessage
	// contains filtered or unexported fields
}

func NewWebsocket

func NewWebsocket(pattern string) *Server

func (*Server) Add

func (s *Server) Add(c *Client)

func (*Server) Clients

func (s *Server) Clients() map[int]*Client

func (*Server) Del

func (s *Server) Del(c *Client)

func (*Server) Done

func (s *Server) Done()

func (*Server) Err

func (s *Server) Err(err error)

func (*Server) Listen

func (s *Server) Listen()

func (*Server) SendAll

func (s *Server) SendAll(msg *Message)

type WebsocketConnection

type WebsocketConnection struct {
	// contains filtered or unexported fields
}

func (*WebsocketConnection) ReadMessage

func (c *WebsocketConnection) ReadMessage() (Message, error)

func (*WebsocketConnection) WriteMessage

func (c *WebsocketConnection) WriteMessage(msg *Message) error

Jump to

Keyboard shortcuts

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