wserver

package
v0.0.9 Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2020 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Overview

Copyright © 2019 Annchain Authors <EMAIL ADDRESS>

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Copyright © 2019 Annchain Authors <EMAIL ADDRESS>

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Copyright © 2019 Annchain Authors <EMAIL ADDRESS>

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Copyright © 2019 Annchain Authors <EMAIL ADDRESS>

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. Package wserver provides building simple websocket server with message push.

Index

Constants

View Source
const (
	EVENT_NEW_UNIT = "new_unit"
)

Variables

View Source
var ErrRequestIllegal = errors.New("request data illegal")

ErrRequestIllegal describes error when data of the request is unaccepted.

Functions

func NewEvent2Cons

func NewEvent2Cons() *event2Cons

Types

type BlockDbUIData added in v0.0.2

type BlockDbUIData struct {
	Nodes []types.TxiSmallCaseMarshal `json:"nodes"`
}

type Conn

type Conn struct {
	Conn *websocket.Conn

	AfterReadFunc   func(messageType int, r io.Reader)
	BeforeCloseFunc func()
	// contains filtered or unexported fields
}

Conn wraps websocket.Conn with Conn. It defines to listen and read data from Conn.

func NewConn

func NewConn(conn *websocket.Conn) *Conn

NewConn wraps conn.

func (*Conn) Close

func (c *Conn) Close() error

Close close the connection.

func (*Conn) GetID

func (c *Conn) GetID() string

GetID returns the id generated using UUID algorithm.

func (*Conn) Listen

func (c *Conn) Listen()

Listen listens for receive data from websocket connection. It blocks until websocket connection is closed.

func (*Conn) Write

func (c *Conn) Write(p []byte) (n int, err error)

Write write p to the websocket connection. The error returned will always be nil if success.

type Edge

type Edge struct {
	Id     string `json:"id"`
	Source string `json:"source"`
	Target string `json:"target"`
}

type Node

type Node struct {
	Data NodeData `json:"data"`
	Type string   `json:"type"`
}

type NodeData

type NodeData struct {
	Unit   string `json:"unit"`
	Unit_s string `json:"unit_s"`
}

type PushMessage

type PushMessage struct {
	//UserID  string `json:"userId"`
	Event   string `json:"event"`
	Message string `json:"message"`
}

PushMessage defines message struct send by client to push to each connected websocket client.

type RegisterMessage

type RegisterMessage struct {
	//Token string
	Event string `json:"event"`
}

RegisterMessage defines message struct client send after connect to the server.

type Server

type Server struct {
	// Address for server to listen on
	Addr string

	// Path for websocket request, default "/ws".
	WSPath string

	// Path for push message, default "/push".
	PushPath string

	// Upgrader is for upgrade connection to websocket connection using
	// "github.com/gorilla/websocket".
	//
	// If Upgrader is nil, default upgrader will be used. Default upgrader is
	// set ReadBufferSize and WriteBufferSize to 1024, and CheckOrigin always
	// returns true.
	Upgrader *websocket.Upgrader

	// Authorize push request. Message will be sent if it returns true,
	// otherwise the request will be discarded. Default nil and push request
	// will always be accepted.
	PushAuth func(r *http.Request) bool

	// To receive new tx events
	NewTxReceivedChan chan types.Txi

	// to receive confirmation events
	BatchConfirmedChan chan map[types2.Hash]types.Txi
	// contains filtered or unexported fields
}

Server defines parameters for running websocket server.

func NewServer

func NewServer(addr string) *Server

NewServer creates a new Server.

func (*Server) GetBenchmarks

func (s *Server) GetBenchmarks() map[string]interface{}

func (*Server) Name

func (s *Server) Name() string

func (*Server) Push

func (s *Server) Push(event, message string) (int, error)

Push filters connections by userID and event, then write message

func (*Server) Serve

func (s *Server) Serve()

ListenAndServe listens on the TCP network address and handle websocket request.

func (*Server) Start

func (s *Server) Start()

func (*Server) Stop

func (s *Server) Stop()

func (*Server) WatchNewTxs

func (s *Server) WatchNewTxs()

type UIData

type UIData struct {
	Type  string `json:"type"`
	Nodes []Node `json:"nodes"`
	Edges []Edge `json:"edges"`
}

func (*UIData) AddToBatch

func (u *UIData) AddToBatch(tx types.Txi, includingEdge bool)

Jump to

Keyboard shortcuts

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