mirror of
https://github.com/kataras/iris.git
synced 2025-12-18 02:17:05 +00:00
Add notes for the new lead maintainer of the open-source iris project and align with @get-ion/ion by @hiveminded
Former-commit-id: da4f38eb9034daa49446df3ee529423b98f9b331
This commit is contained in:
@@ -1,63 +1,74 @@
|
||||
// Copyright 2017 Gerasimos Maropoulos, ΓΜ. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
/*Package websocket provides rich websocket support for the iris web framework.
|
||||
|
||||
Source code and other details for the project are available at GitHub:
|
||||
|
||||
https://github.com/kataras/iris/tree/master/websocket
|
||||
|
||||
Installation
|
||||
|
||||
$ go get -u github.com/kataras/iris/websocket
|
||||
|
||||
|
||||
Example code:
|
||||
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
|
||||
"github.com/kataras/iris/websocket"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
|
||||
app.Get("/", func(ctx context.Context) {
|
||||
ctx.ServeFile("websockets.html", false)
|
||||
})
|
||||
|
||||
setupWebsocket(app)
|
||||
|
||||
// x2
|
||||
// http://localhost:8080
|
||||
// http://localhost:8080
|
||||
// write something, press submit, see the result.
|
||||
app.Run(iris.Addr(":8080"))
|
||||
}
|
||||
|
||||
func setupWebsocket(app *iris.Application) {
|
||||
// create our echo websocket server
|
||||
ws := websocket.New(websocket.Config{
|
||||
ReadBufferSize: 1024,
|
||||
WriteBufferSize: 1024,
|
||||
})
|
||||
ws.OnConnection(handleConnection)
|
||||
|
||||
// register the server's endpoint.
|
||||
// see the inline javascript code in the websockets.html,
|
||||
// this endpoint is used to connect to the server.
|
||||
app.Get("/echo", ws.Handler())
|
||||
|
||||
// serve the javascript built'n client-side library,
|
||||
// see weboskcets.html script tags, this path is used.
|
||||
app.Any("/iris-ws.js", func(ctx context.Context) {
|
||||
ctx.Write(websocket.ClientSource)
|
||||
})
|
||||
}
|
||||
|
||||
func handleConnection(c websocket.Connection) {
|
||||
// Read events from browser
|
||||
c.On("chat", func(msg string) {
|
||||
// Print the message to the console
|
||||
fmt.Printf("%s sent: %s\n", c.Context().RemoteAddr(), msg)
|
||||
// Write message back to the client message owner:
|
||||
// c.Emit("chat", msg)
|
||||
c.To(websocket.Broadcast).Emit("chat", msg)
|
||||
})
|
||||
}
|
||||
|
||||
*/
|
||||
package websocket
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
)
|
||||
|
||||
// New returns a new websocket server policy adaptor.
|
||||
func New(cfg Config) Server {
|
||||
return &server{
|
||||
config: cfg.Validate(),
|
||||
rooms: make(map[string][]string, 0),
|
||||
onConnectionListeners: make([]ConnectionFunc, 0),
|
||||
}
|
||||
}
|
||||
|
||||
func fixPath(s string) string {
|
||||
if s == "" {
|
||||
return ""
|
||||
}
|
||||
|
||||
if s[0] != '/' {
|
||||
s = "/" + s
|
||||
}
|
||||
|
||||
s = strings.Replace(s, "//", "/", -1)
|
||||
return s
|
||||
}
|
||||
|
||||
// Attach adapts the websocket server to one or more Iris instances.
|
||||
func (s *server) Attach(app *iris.Application) {
|
||||
wsPath := fixPath(s.config.Endpoint)
|
||||
if wsPath == "" {
|
||||
app.Log("websocket's configuration field 'Endpoint' cannot be empty, websocket server stops")
|
||||
return
|
||||
}
|
||||
|
||||
wsClientSidePath := fixPath(s.config.ClientSourcePath)
|
||||
if wsClientSidePath == "" {
|
||||
app.Log("websocket's configuration field 'ClientSourcePath' cannot be empty, websocket server stops")
|
||||
return
|
||||
}
|
||||
|
||||
// set the routing for client-side source (javascript) (optional)
|
||||
clientSideLookupName := "iris-websocket-client-side"
|
||||
wsHandler := s.Handler()
|
||||
app.Get(wsPath, wsHandler)
|
||||
// check if client side doesn't already exists
|
||||
if app.GetRoute(clientSideLookupName) == nil {
|
||||
// serve the client side on domain:port/iris-ws.js
|
||||
r, err := app.StaticContent(wsClientSidePath, "application/javascript", ClientSource)
|
||||
if err != nil {
|
||||
app.Log("websocket's route for javascript client-side library failed with: %v", err)
|
||||
return
|
||||
}
|
||||
r.Name = clientSideLookupName
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user