1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-21 20:07:04 +00:00

Add some _examples in the main repository too.

Former-commit-id: 98895c34115ec2076b431332f0ffe9645adf7590
This commit is contained in:
Gerasimos (Makis) Maropoulos
2017-03-13 15:16:12 +02:00
parent d76d9b1ec6
commit f487cd0029
29 changed files with 1318 additions and 248 deletions

View File

@@ -0,0 +1,48 @@
package main
import (
"fmt"
"gopkg.in/kataras/iris.v6"
"gopkg.in/kataras/iris.v6/adaptors/httprouter"
"gopkg.in/kataras/iris.v6/adaptors/websocket"
)
func handleConnection(c websocket.Connection) {
// Read events from browser
c.On("chat", func(msg string) {
// Print the message to the console
fmt.Printf("%s sent: %s\n", c.Context().RemoteAddr(), msg)
// Write message back to browser
c.Emit("chat", msg)
})
}
func main() {
app := iris.New()
app.Adapt(iris.DevLogger())
app.Adapt(httprouter.New())
// create our echo websocket server
ws := websocket.New(websocket.Config{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
Endpoint: "/echo",
})
ws.OnConnection(handleConnection)
// Adapt the websocket server.
// you can adapt more than one of course.
app.Adapt(ws)
app.Get("/", func(ctx *iris.Context) {
ctx.ServeFile("websockets.html", false) // second parameter: enable gzip?
})
app.Listen(":8080")
}

View File

@@ -0,0 +1,29 @@
<input id="input" type="text" />
<button onclick="send()">Send</button>
<pre id="output"></pre>
<script src="/iris-ws.js"></script>
<script>
var input = document.getElementById("input");
var output = document.getElementById("output");
// Ws comes from the auto-served '/iris-ws.js'
var socket = new Ws("ws://localhost:8080/echo");
socket.OnConnect(function () {
output.innerHTML += "Status: Connected\n";
});
socket.OnDisconnect(function () {
output.innerHTML += "Status: Disconnected\n";
});
// read events from the server
socket.On("chat", function (msg) {
output.innerHTML += "Server: " + msg + "\n";
});
function send() {
// send chat event data to the server
socket.Emit("chat", input.value);
input.value = "";
}
</script>