mirror of
https://github.com/kataras/iris.git
synced 2025-12-17 18:07:01 +00:00
Another new feature: websocket controller, for real
Former-commit-id: c1a59b86733e890709b52446e22427a17d87f5fc
This commit is contained in:
82
_examples/mvc/websocket/main.go
Normal file
82
_examples/mvc/websocket/main.go
Normal file
@@ -0,0 +1,82 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"sync/atomic"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/mvc"
|
||||
"github.com/kataras/iris/websocket"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
// load templaes.
|
||||
app.RegisterView(iris.HTML("./views", ".html"))
|
||||
|
||||
// render the ./views/index.html.
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
ctx.View("index.html")
|
||||
})
|
||||
|
||||
mvc.Configure(app.Party("/websocket"), configureMVC)
|
||||
// Or mvc.New(app.Party(...)).Configure(configureMVC)
|
||||
|
||||
// http://localhost:8080
|
||||
app.Run(iris.Addr(":8080"))
|
||||
}
|
||||
|
||||
func configureMVC(m *mvc.Application) {
|
||||
ws := websocket.New(websocket.Config{})
|
||||
// http://localhost:8080/websocket/iris-ws.js
|
||||
m.Router.Any("/iris-ws.js", websocket.ClientHandler())
|
||||
|
||||
// This will bind the result of ws.Upgrade which is a websocket.Connection
|
||||
// to the controller(s) registered via `m.Register`.
|
||||
m.AddDependencies(ws.Upgrade)
|
||||
m.Register(new(websocketController))
|
||||
}
|
||||
|
||||
var visits uint64
|
||||
|
||||
func increment() uint64 {
|
||||
return atomic.AddUint64(&visits, 1)
|
||||
}
|
||||
|
||||
func decrement() uint64 {
|
||||
return atomic.AddUint64(&visits, ^uint64(0))
|
||||
}
|
||||
|
||||
type websocketController struct {
|
||||
// Note that you could use an anonymous field as well, it doesn't matter, binder will find it.
|
||||
//
|
||||
// This is the current websocket connection, each client has its own instance of the *websocketController.
|
||||
Conn websocket.Connection
|
||||
}
|
||||
|
||||
func (c *websocketController) onLeave(roomName string) {
|
||||
// visits--
|
||||
newCount := decrement()
|
||||
// This will call the "visit" event on all clients, except the current one,
|
||||
// (it can't because it's left but for any case use this type of design)
|
||||
c.Conn.To(websocket.Broadcast).Emit("visit", newCount)
|
||||
}
|
||||
|
||||
func (c *websocketController) update() {
|
||||
// visits++
|
||||
newCount := increment()
|
||||
|
||||
// This will call the "visit" event on all clients, incuding the current
|
||||
// with the 'newCount' variable.
|
||||
//
|
||||
// There are many ways that u can do it and faster, for example u can just send a new visitor
|
||||
// and client can increment itself, but here we are just "showcasing" the websocket controller.
|
||||
c.Conn.To(websocket.All).Emit("visit", newCount)
|
||||
}
|
||||
|
||||
func (c *websocketController) Get( /* websocket.Connection could be lived here as well, it doesn't matter */ ) {
|
||||
c.Conn.OnLeave(c.onLeave)
|
||||
c.Conn.On("visit", c.update)
|
||||
|
||||
// call it after all event callbacks registration.
|
||||
c.Conn.Wait()
|
||||
}
|
||||
63
_examples/mvc/websocket/views/index.html
Normal file
63
_examples/mvc/websocket/views/index.html
Normal file
@@ -0,0 +1,63 @@
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<title>Online visitors MVC example</title>
|
||||
<style>
|
||||
body {
|
||||
margin: 0;
|
||||
font-family: -apple-system, "San Francisco", "Helvetica Neue", "Noto", "Roboto", "Calibri Light", sans-serif;
|
||||
color: #212121;
|
||||
font-size: 1.0em;
|
||||
line-height: 1.6;
|
||||
}
|
||||
|
||||
.container {
|
||||
max-width: 750px;
|
||||
margin: auto;
|
||||
padding: 15px;
|
||||
}
|
||||
|
||||
#online_visitors {
|
||||
font-weight: bold;
|
||||
font-size: 18px;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div class="container">
|
||||
<span id="online_visitors">1 online visitor</span>
|
||||
</div>
|
||||
|
||||
<script src="/websocket/iris-ws.js"></script>
|
||||
|
||||
<script type="text/javascript">
|
||||
(function () {
|
||||
var socket = new Ws("ws://localhost:8080/websocket");
|
||||
|
||||
socket.OnConnect(function(){
|
||||
// update the rest of connected clients, including "myself" when "my" connection is 100% ready.
|
||||
socket.Emit("visit");
|
||||
});
|
||||
|
||||
|
||||
socket.On("visit", function (newCount) {
|
||||
console.log("visit websocket event with newCount of: ", newCount);
|
||||
|
||||
var text = "1 online visitor";
|
||||
if (newCount > 1) {
|
||||
text = newCount + " online visitors";
|
||||
}
|
||||
document.getElementById("online_visitors").innerHTML = text;
|
||||
});
|
||||
|
||||
socket.OnDisconnect(function () {
|
||||
document.getElementById("online_visitors").innerHTML = "you've been disconnected";
|
||||
});
|
||||
|
||||
})();
|
||||
</script>
|
||||
|
||||
</body>
|
||||
|
||||
</html>
|
||||
Reference in New Issue
Block a user