2017-03-13 14:16:12 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-07-15 17:45:22 +02:00
|
|
|
"fmt"
|
2017-03-13 14:16:12 +01:00
|
|
|
"sync/atomic"
|
|
|
|
|
2019-10-25 00:27:02 +02:00
|
|
|
"github.com/kataras/iris/v12"
|
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
|
|
|
|
2019-10-25 00:27:02 +02:00
|
|
|
"github.com/kataras/iris/v12/websocket"
|
2017-03-13 14:16:12 +01:00
|
|
|
)
|
|
|
|
|
2019-07-15 17:45:22 +02:00
|
|
|
var events = websocket.Namespaces{
|
|
|
|
"default": websocket.Events{
|
|
|
|
websocket.OnRoomJoined: onRoomJoined,
|
|
|
|
websocket.OnRoomLeft: onRoomLeft,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2018-01-05 06:57:04 +01:00
|
|
|
// load templates
|
2017-07-10 17:32:42 +02:00
|
|
|
app.RegisterView(iris.HTML("./templates", ".html").Reload(true))
|
|
|
|
// setup the websocket server
|
2019-07-15 17:45:22 +02:00
|
|
|
ws := websocket.New(websocket.DefaultGorillaUpgrader, events)
|
2017-03-13 14:16:12 +01:00
|
|
|
|
2019-07-15 17:45:22 +02:00
|
|
|
app.Get("/my_endpoint", websocket.Handler(ws))
|
2017-03-13 14:16:12 +01:00
|
|
|
|
2017-07-10 17:32:42 +02:00
|
|
|
// register static assets request path and system directory
|
2020-07-24 12:03:49 +02:00
|
|
|
app.HandleDir("/js", iris.Dir("./static/assets/js"))
|
2017-03-13 14:16:12 +01:00
|
|
|
|
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"})
|
2022-12-13 00:37:15 +01:00
|
|
|
if err := ctx.View("index.html"); err != nil {
|
|
|
|
ctx.HTML("<h3>%s</h3>", err.Error())
|
|
|
|
return
|
|
|
|
}
|
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"})
|
2022-12-13 00:37:15 +01:00
|
|
|
if err := ctx.View("other.html"); err != nil {
|
|
|
|
ctx.HTML("<h3>%s</h3>", err.Error())
|
|
|
|
return
|
|
|
|
}
|
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)
|
2020-03-05 21:41:27 +01:00
|
|
|
app.Listen(":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() {
|
2018-03-08 04:21:16 +01:00
|
|
|
atomic.AddUint64(&v.count, ^uint64(0))
|
2017-03-13 14:16:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *pageView) getCount() uint64 {
|
2018-03-08 04:21:16 +01:00
|
|
|
return atomic.LoadUint64(&v.count)
|
2017-03-13 14:16:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2019-07-15 17:45:22 +02:00
|
|
|
func viewsCountBytes(viewsCount uint64) []byte {
|
|
|
|
// * there are other methods to convert uint64 to []byte
|
|
|
|
return []byte(fmt.Sprintf("%d", viewsCount))
|
|
|
|
}
|
2017-03-13 14:16:12 +01:00
|
|
|
|
2019-07-15 17:45:22 +02:00
|
|
|
func onRoomJoined(ns *websocket.NSConn, msg websocket.Message) error {
|
|
|
|
// the roomName here is the source.
|
|
|
|
pageSource := string(msg.Room)
|
2017-03-13 14:16:12 +01:00
|
|
|
|
2019-07-15 17:45:22 +02:00
|
|
|
v.Add(pageSource)
|
|
|
|
|
|
|
|
viewsCount := v.Get(pageSource).getCount()
|
|
|
|
if viewsCount == 0 {
|
|
|
|
viewsCount++ // count should be always > 0 here
|
|
|
|
}
|
|
|
|
|
|
|
|
// fire the "onNewVisit" client event
|
|
|
|
// on each connection joined to this room (source page)
|
|
|
|
// and notify of the new visit,
|
|
|
|
// including this connection (see nil on first input arg).
|
|
|
|
ns.Conn.Server().Broadcast(nil, websocket.Message{
|
|
|
|
Namespace: msg.Namespace,
|
|
|
|
Room: pageSource,
|
|
|
|
Event: "onNewVisit", // fire the "onNewVisit" client event.
|
|
|
|
Body: viewsCountBytes(viewsCount),
|
2017-03-13 14:16:12 +01:00
|
|
|
})
|
|
|
|
|
2019-07-15 17:45:22 +02:00
|
|
|
return nil
|
|
|
|
}
|
2017-03-13 14:16:12 +01:00
|
|
|
|
2019-07-15 17:45:22 +02:00
|
|
|
func onRoomLeft(ns *websocket.NSConn, msg websocket.Message) error {
|
|
|
|
// the roomName here is the source.
|
|
|
|
pageV := v.Get(msg.Room)
|
|
|
|
if pageV == nil {
|
|
|
|
return nil // for any case that this room is not a pageView source
|
|
|
|
}
|
|
|
|
// decrement -1 the specific counter for this page source.
|
|
|
|
pageV.decrement()
|
|
|
|
|
|
|
|
// fire the "onNewVisit" client event
|
|
|
|
// on each connection joined to this room (source page)
|
|
|
|
// and notify of the new, decremented by one, visits count.
|
|
|
|
ns.Conn.Server().Broadcast(nil, websocket.Message{
|
|
|
|
Namespace: msg.Namespace,
|
|
|
|
Room: msg.Room,
|
|
|
|
Event: "onNewVisit",
|
|
|
|
Body: viewsCountBytes(pageV.getCount()),
|
2017-03-13 14:16:12 +01:00
|
|
|
})
|
2019-07-15 17:45:22 +02:00
|
|
|
|
|
|
|
return nil
|
2017-03-13 14:16:12 +01:00
|
|
|
}
|