2017-02-15 07:40:43 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
Publish the new version :airplane: | Look description please!
# 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
2017-06-03 22:22:52 +02:00
|
|
|
"github.com/kataras/iris"
|
|
|
|
"github.com/kataras/iris/context"
|
|
|
|
|
|
|
|
"github.com/kataras/iris/view"
|
|
|
|
"github.com/kataras/iris/websocket"
|
2017-02-15 07:40:43 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type clientPage struct {
|
|
|
|
Title string
|
|
|
|
Host string
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
app := iris.New()
|
Publish the new version :airplane: | Look description please!
# 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
2017-06-03 22:22:52 +02:00
|
|
|
app.AttachView(view.HTML("./templates", ".html")) // select the html engine to serve templates
|
2017-02-15 07:40:43 +01:00
|
|
|
|
|
|
|
ws := websocket.New(websocket.Config{
|
Publish the new version :airplane: | Look description please!
# 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
2017-06-03 22:22:52 +02:00
|
|
|
// the request path which the websocket client should listen/registered to the server,
|
|
|
|
// the endpoint is like a route.
|
2017-02-15 07:40:43 +01:00
|
|
|
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):
|
Publish the new version :airplane: | Look description please!
# 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
2017-06-03 22:22:52 +02:00
|
|
|
// IDGenerator func(ctx context.Context) string {},
|
2017-02-15 07:40:43 +01:00
|
|
|
})
|
|
|
|
|
Publish the new version :airplane: | Look description please!
# 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
2017-06-03 22:22:52 +02:00
|
|
|
ws.Attach(app) // adapt the application to the websocket server
|
2017-02-15 07:40:43 +01:00
|
|
|
|
|
|
|
app.StaticWeb("/js", "./static/js") // serve our custom javascript code
|
|
|
|
|
Publish the new version :airplane: | Look description please!
# 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
2017-06-03 22:22:52 +02:00
|
|
|
app.Get("/", func(ctx context.Context) {
|
|
|
|
ctx.ViewData("", clientPage{"Client Page", "localhost:8080"})
|
|
|
|
ctx.View("client.html")
|
2017-02-15 07:40:43 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
Conn := make(map[websocket.Connection]bool)
|
|
|
|
var myChatRoom = "room1"
|
|
|
|
var mutex = new(sync.Mutex)
|
|
|
|
|
|
|
|
ws.OnConnection(func(c websocket.Connection) {
|
|
|
|
c.Join(myChatRoom)
|
|
|
|
mutex.Lock()
|
|
|
|
Conn[c] = true
|
|
|
|
mutex.Unlock()
|
|
|
|
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
|
|
|
|
}
|
|
|
|
})
|
|
|
|
c.OnDisconnect(func() {
|
|
|
|
mutex.Lock()
|
|
|
|
delete(Conn, c)
|
|
|
|
mutex.Unlock()
|
|
|
|
fmt.Printf("\nConnection with ID: %s has been disconnected!\n", c.ID())
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
var delay = 1 * time.Second
|
|
|
|
go func() {
|
|
|
|
i := 0
|
|
|
|
for {
|
|
|
|
mutex.Lock()
|
|
|
|
broadcast(Conn, fmt.Sprintf("aaaa %d\n", i))
|
|
|
|
mutex.Unlock()
|
|
|
|
time.Sleep(delay)
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
i := 0
|
|
|
|
for {
|
|
|
|
mutex.Lock()
|
|
|
|
broadcast(Conn, fmt.Sprintf("aaaa2 %d\n", i))
|
|
|
|
mutex.Unlock()
|
|
|
|
time.Sleep(delay)
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
Publish the new version :airplane: | Look description please!
# 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
2017-06-03 22:22:52 +02:00
|
|
|
app.Run(iris.Addr(":8080"))
|
2017-02-15 07:40:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func broadcast(Conn map[websocket.Connection]bool, message string) {
|
|
|
|
for k := range Conn {
|
|
|
|
k.To("room1").Emit("chat", message)
|
|
|
|
}
|
|
|
|
}
|