func (s *NGServer) sync(c *NGClient) {
	for _, ev := range s.eventBuffer {
		c.eventChan <- ev
	}
}
/*
If the flag '-s' is set and the browser sent a 'sync' command,
the NGServer will push all the http message buffered in eventBuffer to
the client.
package main
import (
	"encoding/json"
	"log"
	"net/http"
	"os"
	"sync"
	"github.com/ga0/netgraph/web"
	"golang.org/x/net/websocket"
)
type NGClient struct {
	eventChan chan interface{}
	server    *NGServer
	ws        *websocket.Conn
}
func (c *NGClient) recvAndProcessCommand() {
	for {
		var msg string
		err := websocket.Message.Receive(c.ws, &msg)
		if err != nil {
			return
		}
		if len(msg) > 0 {
			if msg == "sync" {
				c.server.sync(c)
			}
		} else {
			panic("empty command")
		}
	}
}
func (c *NGClient) transmitEvents() {
	for ev := range c.eventChan {
		json, err := json.Marshal(ev)
		if err == nil {
			websocket.Message.Send(c.ws, string(json))
		}
	}
}
func (c *NGClient) close() {
	close(c.eventChan)
}
func NewNGClient(ws *websocket.Conn, server *NGServer) *NGClient {
	c := new(NGClient)
	c.server = server
	c.ws = ws
	c.eventChan = make(chan interface{}, 16)
	return c
}
type NGServer struct {
	addr                 string
	staticFileDir        string
	connectedClient      map[*websocket.Conn]*NGClient
	connectedClientMutex *sync.Mutex
	eventBuffer          []interface{}
	saveEvent            bool
	wg                   sync.WaitGroup
}
func (s *NGServer) websocketHandler(ws *websocket.Conn) {
	c := NewNGClient(ws, s)
	s.connectedClientMutex.Lock()
	s.connectedClient[ws] = c
	s.connectedClientMutex.Unlock()
	go c.transmitEvents()
	c.recvAndProcessCommand()
	c.close()
	s.connectedClientMutex.Lock()
	delete(s.connectedClient, ws)
	s.connectedClientMutex.Unlock()
}
func (s *NGServer) PushEvent(e interface{}) {
	if s.saveEvent {
		s.eventBuffer = append(s.eventBuffer, e)
	}
	s.connectedClientMutex.Lock()
	for _, c := range s.connectedClient {
		c.eventChan <- e
	}
	s.connectedClientMutex.Unlock()
}
func (s *NGServer) Wait() {
	s.wg.Wait()
}