mirror of
https://github.com/kataras/iris.git
synced 2025-01-24 11:11:03 +01:00
5e4b63acb2
# FAQ ### Looking for free support? http://support.iris-go.com https://kataras.rocket.chat/channel/iris ### Looking for previous versions? https://github.com/kataras/iris#version ### Should I upgrade my Iris? Developers are not forced to upgrade if they don't really need it. Upgrade whenever you feel ready. > Iris uses the [vendor directory](https://docs.google.com/document/d/1Bz5-UB7g2uPBdOx-rw5t9MxJwkfpx90cqG9AFL0JAYo) feature, so you get truly reproducible builds, as this method guards against upstream renames and deletes. **How to upgrade**: Open your command-line and execute this command: `go get -u github.com/kataras/iris`. For further installation support, please click [here](http://support.iris-go.com/d/16-how-to-install-iris-web-framework). ### About our new home page http://iris-go.com Thanks to [Santosh Anand](https://github.com/santoshanand) the http://iris-go.com has been upgraded and it's really awesome! [Santosh](https://github.com/santoshanand) is a freelancer, he has a great knowledge of nodejs and express js, Android, iOS, React Native, Vue.js etc, if you need a developer to find or create a solution for your problem or task, please contact with him. The amount of the next two or three donations you'll send they will be immediately transferred to his own account balance, so be generous please! Read more at https://github.com/kataras/iris/blob/master/HISTORY.md Former-commit-id: eec2d71bbe011d6b48d2526eb25919e36e5ad94e
101 lines
3.2 KiB
Go
101 lines
3.2 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt" // optional
|
|
|
|
"github.com/kataras/iris"
|
|
"github.com/kataras/iris/context"
|
|
|
|
"github.com/kataras/iris/view"
|
|
"github.com/kataras/iris/websocket"
|
|
)
|
|
|
|
type clientPage struct {
|
|
Title string
|
|
Host string
|
|
}
|
|
|
|
func main() {
|
|
app := iris.New()
|
|
app.AttachView(view.HTML("./templates", ".html")) // select the html engine to serve templates
|
|
|
|
ws := websocket.New(websocket.Config{
|
|
// the request path which the websocket client should listen/registered to the server,
|
|
// the endpoint is like a route.
|
|
Endpoint: "/my_endpoint",
|
|
// the client-side javascript static file path
|
|
// which will be served by Iris.
|
|
// default is /iris-ws.js
|
|
// if you change that you have to change the bottom of templates/client.html
|
|
// script tag:
|
|
ClientSourcePath: "/iris-ws.js",
|
|
//
|
|
// Set the timeouts, 0 means no timeout
|
|
// websocket has more configuration, go to ../../config.go for more:
|
|
// WriteTimeout: 0,
|
|
// ReadTimeout: 0,
|
|
// by-default all origins are accepted, you can change this behavior by setting:
|
|
// CheckOrigin: (r *http.Request ) bool {},
|
|
//
|
|
//
|
|
// IDGenerator used to create (and later on, set)
|
|
// an ID for each incoming websocket connections (clients).
|
|
// The request is an argument which you can use to generate the ID (from headers for example).
|
|
// If empty then the ID is generated by DefaultIDGenerator: randomString(64):
|
|
// IDGenerator func(ctx context.Context) string {},
|
|
})
|
|
|
|
ws.Attach(app) // adapt the websocket server, you can adapt more than one with different Endpoint
|
|
|
|
app.StaticWeb("/js", "./static/js") // serve our custom javascript code
|
|
|
|
app.Get("/", func(ctx context.Context) {
|
|
ctx.ViewData("", clientPage{"Client Page", ctx.Host()})
|
|
ctx.View("client.html")
|
|
})
|
|
|
|
var myChatRoom = "room1"
|
|
|
|
ws.OnConnection(func(c websocket.Connection) {
|
|
// Context returns the (upgraded) context.Context of this connection
|
|
// avoid using it, you normally don't need it,
|
|
// websocket has everything you need to authenticate the user BUT if it's necessary
|
|
// then you use it to receive user information, for example: from headers.
|
|
|
|
// ctx := c.Context()
|
|
|
|
// join to a room (optional)
|
|
c.Join(myChatRoom)
|
|
|
|
c.On("chat", func(message string) {
|
|
if message == "leave" {
|
|
c.Leave(myChatRoom)
|
|
c.To(myChatRoom).Emit("chat", "Client with ID: "+c.ID()+" left from the room and cannot send or receive message to/from this room.")
|
|
c.Emit("chat", "You have left from the room: "+myChatRoom+" you cannot send or receive any messages from others inside that room.")
|
|
return
|
|
}
|
|
// to all except this connection ->
|
|
// c.To(websocket.Broadcast).Emit("chat", "Message from: "+c.ID()+"-> "+message)
|
|
// to all connected clients: c.To(websocket.All)
|
|
|
|
// to the client itself ->
|
|
//c.Emit("chat", "Message from myself: "+message)
|
|
|
|
//send the message to the whole room,
|
|
//all connections are inside this room will receive this message
|
|
c.To(myChatRoom).Emit("chat", "From: "+c.ID()+": "+message)
|
|
})
|
|
|
|
// or create a new leave event
|
|
// c.On("leave", func() {
|
|
// c.Leave(myChatRoom)
|
|
// })
|
|
|
|
c.OnDisconnect(func() {
|
|
fmt.Printf("Connection with ID: %s has been disconnected!\n", c.ID())
|
|
})
|
|
})
|
|
|
|
app.Run(iris.Addr(":8080"))
|
|
}
|