2017-03-13 14:16:12 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync/atomic"
|
|
|
|
|
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/websocket"
|
2017-03-13 14:16:12 +01:00
|
|
|
)
|
|
|
|
|
2017-07-10 17:32:42 +02:00
|
|
|
func main() {
|
|
|
|
// init the web application instance
|
|
|
|
// app := iris.New()
|
|
|
|
app := iris.Default()
|
2017-03-13 14:16:12 +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
|
|
|
// load templaes
|
2017-07-10 17:32:42 +02:00
|
|
|
app.RegisterView(iris.HTML("./templates", ".html").Reload(true))
|
|
|
|
// setup the websocket server
|
|
|
|
ws := websocket.New(websocket.Config{})
|
2017-03-13 14:16:12 +01:00
|
|
|
ws.OnConnection(HandleWebsocketConnection)
|
|
|
|
|
2017-07-10 17:32:42 +02:00
|
|
|
app.Get("/my_endpoint", ws.Handler())
|
|
|
|
app.Any("/iris-ws.js", websocket.ClientHandler())
|
2017-03-13 14:16:12 +01:00
|
|
|
|
2017-07-10 17:32:42 +02:00
|
|
|
// register static assets request path and system directory
|
2017-03-13 14:16:12 +01:00
|
|
|
app.StaticWeb("/js", "./static/assets/js")
|
|
|
|
|
2017-08-27 19:35:23 +02:00
|
|
|
h := func(ctx iris.Context) {
|
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
|
|
|
ctx.ViewData("", page{PageID: "index page"})
|
|
|
|
ctx.View("index.html")
|
2017-03-13 14:16:12 +01:00
|
|
|
}
|
|
|
|
|
2017-08-27 19:35:23 +02:00
|
|
|
h2 := func(ctx iris.Context) {
|
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
|
|
|
ctx.ViewData("", page{PageID: "other page"})
|
|
|
|
ctx.View("other.html")
|
2017-03-13 14:16:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Open some browser tabs/or windows
|
|
|
|
// and navigate to
|
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
|
|
|
// http://localhost:8080/ and http://localhost:8080/other multiple times.
|
2017-03-13 14:16:12 +01:00
|
|
|
// Each page has its own online-visitors counter.
|
|
|
|
app.Get("/", h)
|
|
|
|
app.Get("/other", h2)
|
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-03-13 14:16:12 +01:00
|
|
|
}
|
|
|
|
|
2017-07-10 17:32:42 +02:00
|
|
|
type page struct {
|
|
|
|
PageID string
|
|
|
|
}
|
|
|
|
|
2017-03-13 14:16:12 +01:00
|
|
|
type pageView struct {
|
|
|
|
source string
|
|
|
|
count uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *pageView) increment() {
|
|
|
|
atomic.AddUint64(&v.count, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *pageView) decrement() {
|
|
|
|
oldCount := v.count
|
|
|
|
if oldCount > 0 {
|
|
|
|
atomic.StoreUint64(&v.count, oldCount-1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *pageView) getCount() uint64 {
|
|
|
|
val := atomic.LoadUint64(&v.count)
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
|
|
|
|
type (
|
|
|
|
pageViews []pageView
|
|
|
|
)
|
|
|
|
|
|
|
|
func (v *pageViews) Add(source string) {
|
|
|
|
args := *v
|
|
|
|
n := len(args)
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
kv := &args[i]
|
|
|
|
if kv.source == source {
|
|
|
|
kv.increment()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c := cap(args)
|
|
|
|
if c > n {
|
|
|
|
args = args[:n+1]
|
|
|
|
kv := &args[n]
|
|
|
|
kv.source = source
|
|
|
|
kv.count = 1
|
|
|
|
*v = args
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
kv := pageView{}
|
|
|
|
kv.source = source
|
|
|
|
kv.count = 1
|
|
|
|
*v = append(args, kv)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *pageViews) Get(source string) *pageView {
|
|
|
|
args := *v
|
|
|
|
n := len(args)
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
kv := &args[i]
|
|
|
|
if kv.source == source {
|
|
|
|
return kv
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *pageViews) Reset() {
|
|
|
|
*v = (*v)[:0]
|
|
|
|
}
|
|
|
|
|
|
|
|
var v pageViews
|
|
|
|
|
|
|
|
// HandleWebsocketConnection handles the online viewers per example(gist source)
|
|
|
|
func HandleWebsocketConnection(c websocket.Connection) {
|
|
|
|
|
|
|
|
c.On("watch", func(pageSource string) {
|
|
|
|
v.Add(pageSource)
|
|
|
|
// join the socket to a room linked with the page source
|
|
|
|
c.Join(pageSource)
|
|
|
|
|
|
|
|
viewsCount := v.Get(pageSource).getCount()
|
|
|
|
if viewsCount == 0 {
|
|
|
|
viewsCount++ // count should be always > 0 here
|
|
|
|
}
|
|
|
|
c.To(pageSource).Emit("watch", viewsCount)
|
|
|
|
})
|
|
|
|
|
|
|
|
c.OnLeave(func(roomName string) {
|
|
|
|
if roomName != c.ID() { // if the roomName it's not the connection iself
|
|
|
|
// the roomName here is the source, this is the only room(except the connection's ID room) which we join the users to.
|
|
|
|
pageV := v.Get(roomName)
|
|
|
|
if pageV == nil {
|
|
|
|
return // for any case that this room is not a pageView source
|
|
|
|
}
|
|
|
|
// decrement -1 the specific counter for this page source.
|
|
|
|
pageV.decrement()
|
|
|
|
// 1. open 30 tabs.
|
|
|
|
// 2. close the browser.
|
|
|
|
// 3. re-open the browser
|
|
|
|
// 4. should be v.getCount() = 1
|
|
|
|
// in order to achieve the previous flow we should decrement exactly when the user disconnects
|
|
|
|
// but emit the result a little after, on a goroutine
|
|
|
|
// getting all connections within this room and emit the online views one by one.
|
|
|
|
// note:
|
|
|
|
// we can also add a time.Sleep(2-3 seconds) inside the goroutine at the future if we don't need 'real-time' updates.
|
|
|
|
go func(currentConnID string) {
|
2017-07-10 17:32:42 +02:00
|
|
|
for _, conn := range c.Server().GetConnectionsByRoom(roomName) {
|
2017-03-13 14:16:12 +01:00
|
|
|
if conn.ID() != currentConnID {
|
|
|
|
conn.Emit("watch", pageV.getCount())
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}(c.ID())
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|