Exemplo n.º 1
0
// newServer creates a websocket server and returns it
func newServer(c *config.Websocket) *server {
	s := &server{
		config:                c,
		put:                   make(chan *connection),
		free:                  make(chan *connection),
		connections:           make(map[string]*connection),
		join:                  make(chan roomPayload, 1), // buffered because join can be called immediately on connection connected
		leave:                 make(chan roomPayload),
		rooms:                 make(Rooms),
		messages:              make(chan messagePayload, 1), // buffered because messages can be sent/received immediately on connection connected
		onConnectionListeners: make([]ConnectionFunc, 0),
	}

	s.upgrader = websocket.Custom(s.handleConnection, s.config.ReadBufferSize, s.config.WriteBufferSize, false)
	go s.serve() // start the server automatically
	return s
}
Exemplo n.º 2
0
// RegisterTo creates the client side source route and the route path Endpoint with the correct Handler
// receives the websocket configuration and  the iris station
func (s *WebsocketServer) RegisterTo(station *Framework, c WebsocketConfiguration) {

	// Note: s.Server should be initialize on the first OnConnection, which is called before this func  when Default websocket server.
	// When not: when calling this function before OnConnection, when we have more than one websocket server running
	if s.Server == nil {
		s.Server = websocket.New()
	}
	// is just a conversional type for kataras/go-websocket.Connection
	s.upgrader = irisWebsocket.Custom(s.Server.HandleConnection, c.ReadBufferSize, c.WriteBufferSize, c.Headers)

	// set the routing for client-side source (javascript) (optional)
	clientSideLookupName := "iris-websocket-client-side"
	station.Get(c.Endpoint, s.Handler)
	// check if client side already exists
	if station.Lookup(clientSideLookupName) == nil {
		// serve the client side on domain:port/iris-ws.js
		station.StaticContent("/iris-ws.js", contentJavascript, websocket.ClientSource)(clientSideLookupName)
	}

	s.Server.Set(websocket.Config{
		WriteTimeout:    c.WriteTimeout,
		PongTimeout:     c.PongTimeout,
		PingPeriod:      c.PingPeriod,
		MaxMessageSize:  c.MaxMessageSize,
		BinaryMessages:  c.BinaryMessages,
		ReadBufferSize:  c.ReadBufferSize,
		WriteBufferSize: c.WriteBufferSize,
	})

	s.Error = c.Error
	s.CheckOrigin = c.CheckOrigin

	if s.Error == nil {
		s.Error = DefaultWebsocketError
	}

	if s.CheckOrigin == nil {
		s.CheckOrigin = DefaultWebsocketCheckOrigin
	}

	// run the ws server
	s.Server.Serve()

}