mirror of
https://github.com/kataras/iris.git
synced 2025-12-17 18:07:01 +00:00
Add the new websocket package (which is just a helper for kataras/neffos) and an example for go server, client, browser client and nodejs client. Add a .fossa.yml and the generated NOTICE file for 3rd-party libs. Update go.mod, go.sum. Update the vendor folder for pongo2 to its latest master as well
Former-commit-id: 89c05079415977d65e7328a1eb8a1c602d76f78a
This commit is contained in:
53
_examples/websocket/basic/server.go
Normal file
53
_examples/websocket/basic/server.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/websocket"
|
||||
)
|
||||
|
||||
const namespace = "default"
|
||||
|
||||
// if namespace is empty then simply websocket.Events{...} can be used instead.
|
||||
var serverEvents = websocket.Namespaces{
|
||||
namespace: websocket.Events{
|
||||
websocket.OnNamespaceConnected: func(c *websocket.NSConn, msg websocket.Message) error {
|
||||
log.Printf("[%s] connected to namespace [%s]", c, msg.Namespace)
|
||||
return nil
|
||||
},
|
||||
websocket.OnNamespaceDisconnect: func(c *websocket.NSConn, msg websocket.Message) error {
|
||||
log.Printf("[%s] disconnected from namespace [%s]", c, msg.Namespace)
|
||||
return nil
|
||||
},
|
||||
"chat": func(c *websocket.NSConn, msg websocket.Message) error {
|
||||
log.Printf("[%s] sent: %s", c.Conn.ID(), string(msg.Body))
|
||||
|
||||
// Write message back to the client message owner with:
|
||||
// c.Emit("chat", msg)
|
||||
// Write message to all except this client with:
|
||||
c.Conn.Server().Broadcast(c, msg)
|
||||
return nil
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
websocketServer := websocket.New(
|
||||
websocket.DefaultGorillaUpgrader, /*DefaultGobwasUpgrader can be used as well*/
|
||||
serverEvents)
|
||||
|
||||
// serves the endpoint of ws://localhost:8080/echo
|
||||
app.Get("/echo", websocket.Handler(websocketServer))
|
||||
|
||||
// serves the browser-based websocket client.
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
ctx.ServeFile("./browser/index.html", false)
|
||||
})
|
||||
|
||||
// serves the npm browser websocket client usage example.
|
||||
app.StaticWeb("/browserify", "./browserify")
|
||||
|
||||
app.Run(iris.Addr(":8080"))
|
||||
}
|
||||
Reference in New Issue
Block a user