Documentation ¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client maintains a mapping to the server, sites and websocket connection.
func (*Client) Broadcast ¶
func (c *Client) Broadcast()
Broadcast processes server messages to the websocket client connection.
Broadcast should always be started as a goroutine for each websocket client which handles all the information sending to the client.
func (*Client) CheckSites ¶
func (c *Client) CheckSites()
CheckSites iterates over the client sites to perform health check.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server contains information about the server.
Example ¶
package main import ( "fmt" "net/http" "net/http/httptest" "os" "path" "runtime" "strings" "github.com/gorilla/websocket" "github.com/lordmangila/status-checker/pkg/rest" ) var rs *rest.Server func init() { _, filename, _, _ := runtime.Caller(0) dir := path.Join(path.Dir(filename), "../..") err := os.Chdir(dir) if err != nil { panic(err) } rs = rest.NewServer() go rs.Run() rs.SetRoutes() go func() { rs.ListenListenAndServe() }() } func main() { // Create a new test Server. server := httptest.NewServer(http.HandlerFunc(rs.ServeWS)) defer server.Close() // Connect to the websocket address. wsAddr := "ws" + strings.TrimPrefix(server.URL, "http") conn, _, err := websocket.DefaultDialer.Dial(wsAddr, nil) if nil != err { conn.Close() } // Send an invalid url to the server. conn.WriteMessage(1, []byte("invalidurl")) // Read failed response from the websocket. _, message, _ := conn.ReadMessage() fmt.Println(string(message)) // Register https://www.google.com/ to the client's sites. conn.WriteMessage(1, []byte("https://www.google.com/")) // Read response from the websocket for google. _, message, _ = conn.ReadMessage() fmt.Println(string(message)) // Register https://www.github.com/ to the client's sites. conn.WriteMessage(1, []byte("https://www.github.com/")) // This will be done twice as there are already 2 sites registered to the client. // Read response from the websocket. // 1st response will show google status. _, message, _ = conn.ReadMessage() fmt.Println(string(message)) // Read response from the websocket. // 2nd response will show github status. _, message, _ = conn.ReadMessage() fmt.Println(string(message)) }
Output: {"URL":"invalidurl","StatusCode":0,"Active":false,"Valid":false,"Error":"Invalid URI: invalidurl"} {"URL":"https://www.google.com/","StatusCode":200,"Active":true,"Valid":true,"Error":""} {"URL":"https://www.google.com/","StatusCode":200,"Active":true,"Valid":true,"Error":""} {"URL":"https://www.github.com/","StatusCode":200,"Active":true,"Valid":true,"Error":""}
func (*Server) ListenListenAndServe ¶
func (s *Server) ListenListenAndServe()
ListenListenAndServe initiates the handlers.
Click to show internal directories.
Click to hide internal directories.