mirror of
https://github.com/kataras/iris.git
synced 2026-01-09 21:15:56 +00:00
update the nantive-messages(only) example to the latest websocket (minimum changes, the idea is the same) and misc
Former-commit-id: 9598319bc13e8a383114c37f4da84f337ab47b22
This commit is contained in:
@@ -685,6 +685,25 @@ The `iris.Context` source code can be found [here](https://github.com/kataras/ir
|
||||
// context.Context is very extensible and developers can override
|
||||
// its methods if that is actually needed.
|
||||
type Context interface {
|
||||
// BeginRequest is executing once for each request
|
||||
// it should prepare the (new or acquired from pool) context's fields for the new request.
|
||||
//
|
||||
// To follow the iris' flow, developer should:
|
||||
// 1. reset handlers to nil
|
||||
// 2. reset values to empty
|
||||
// 3. reset sessions to nil
|
||||
// 4. reset response writer to the http.ResponseWriter
|
||||
// 5. reset request to the *http.Request
|
||||
// and any other optional steps, depends on dev's application type.
|
||||
BeginRequest(http.ResponseWriter, *http.Request)
|
||||
// EndRequest is executing once after a response to the request was sent and this context is useless or released.
|
||||
//
|
||||
// To follow the iris' flow, developer should:
|
||||
// 1. flush the response writer's result
|
||||
// 2. release the response writer
|
||||
// and any other optional steps, depends on dev's application type.
|
||||
EndRequest()
|
||||
|
||||
// ResponseWriter returns an http.ResponseWriter compatible response writer, as expected.
|
||||
ResponseWriter() ResponseWriter
|
||||
// ResetResponseWriter should change or upgrade the Context's ResponseWriter.
|
||||
@@ -692,6 +711,18 @@ type Context interface {
|
||||
|
||||
// Request returns the original *http.Request, as expected.
|
||||
Request() *http.Request
|
||||
// ResetRequest sets the Context's Request,
|
||||
// It is useful to store the new request created by a std *http.Request#WithContext() into Iris' Context.
|
||||
// Use `ResetRequest` when for some reason you want to make a full
|
||||
// override of the *http.Request.
|
||||
// Note that: when you just want to change one of each fields you can use the Request() which returns a pointer to Request,
|
||||
// so the changes will have affect without a full override.
|
||||
// Usage: you use a native http handler which uses the standard "context" package
|
||||
// to get values instead of the Iris' Context#Values():
|
||||
// r := ctx.Request()
|
||||
// stdCtx := context.WithValue(r.Context(), key, val)
|
||||
// ctx.ResetRequest(r.WithContext(stdCtx)).
|
||||
ResetRequest(r *http.Request)
|
||||
|
||||
// SetCurrentRouteName sets the route's name internally,
|
||||
// in order to be able to find the correct current "read-only" Route when
|
||||
@@ -732,7 +763,7 @@ type Context interface {
|
||||
// HandlerIndex sets the current index of the
|
||||
// current context's handlers chain.
|
||||
// If -1 passed then it just returns the
|
||||
// current handler index without change the current index.rns that index, useless return value.
|
||||
// current handler index without change the current index.
|
||||
//
|
||||
// Look Handlers(), Next() and StopExecution() too.
|
||||
HandlerIndex(n int) (currentIndex int)
|
||||
@@ -775,6 +806,12 @@ type Context interface {
|
||||
Proceed(Handler) bool
|
||||
// HandlerName returns the current handler's name, helpful for debugging.
|
||||
HandlerName() string
|
||||
// HandlerFileLine returns the current running handler's function source file and line information.
|
||||
// Useful mostly when debugging.
|
||||
HandlerFileLine() (file string, line int)
|
||||
// RouteName returns the route name that this handler is running on.
|
||||
// Note that it will return empty on not found handlers.
|
||||
RouteName() string
|
||||
// Next calls all the next handler from the handlers chain,
|
||||
// it should be used inside a middleware.
|
||||
//
|
||||
@@ -853,7 +890,8 @@ type Context interface {
|
||||
// that can be used to share information between handlers and middleware.
|
||||
Values() *memstore.Store
|
||||
// Translate is the i18n (localization) middleware's function,
|
||||
// it calls the Get("translate") to return the translated value.
|
||||
// it calls the Values().Get(ctx.Application().ConfigurationReadOnly().GetTranslateFunctionContextKey())
|
||||
// to execute the translate function and return the localized text value.
|
||||
//
|
||||
// Example: https://github.com/kataras/iris/tree/master/_examples/miscellaneous/i18n
|
||||
Translate(format string, args ...interface{}) string
|
||||
@@ -870,7 +908,6 @@ type Context interface {
|
||||
// RequestPath returns the full request path,
|
||||
// based on the 'escape'.
|
||||
RequestPath(escape bool) string
|
||||
|
||||
// Host returns the host part of the current url.
|
||||
Host() string
|
||||
// Subdomain returns the subdomain of this request, if any.
|
||||
@@ -878,6 +915,9 @@ type Context interface {
|
||||
Subdomain() (subdomain string)
|
||||
// IsWWW returns true if the current subdomain (if any) is www.
|
||||
IsWWW() bool
|
||||
// FullRqeuestURI returns the full URI,
|
||||
// including the scheme, the host and the relative requested path/resource.
|
||||
FullRequestURI() string
|
||||
// RemoteAddr tries to parse and return the real client's request IP.
|
||||
//
|
||||
// Based on allowed headers names that can be modified from Configuration.RemoteAddrHeaders.
|
||||
@@ -1189,7 +1229,7 @@ type Context interface {
|
||||
// Note that it has nothing to do with server-side caching.
|
||||
// It does those checks by checking if the "If-Modified-Since" request header
|
||||
// sent by client or a previous server response header
|
||||
// (e.g with WriteWithExpiration or StaticEmbedded or Favicon etc.)
|
||||
// (e.g with WriteWithExpiration or HandleDir or Favicon etc.)
|
||||
// is a valid one and it's before the "modtime".
|
||||
//
|
||||
// A check for !modtime && err == nil is necessary to make sure that
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
However, `neffos.(min.)js` is a NPM package too so alternatively,
|
||||
you can use it as dependency on your package.json and all nodejs-npm tooling become available:
|
||||
see the "browserify" example for more-->
|
||||
<script src="https://cdn.jsdelivr.net/npm/neffos.js@0.1.12/dist/neffos.min.js"></script>
|
||||
<script src="https://cdn.jsdelivr.net/npm/neffos.js@latest/dist/neffos.min.js"></script>
|
||||
<script>
|
||||
// `neffos` global variable is available now.
|
||||
var scheme = document.location.protocol == "https:" ? "wss" : "ws";
|
||||
|
||||
@@ -1,21 +1,13 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
|
||||
"github.com/kataras/iris/websocket"
|
||||
)
|
||||
|
||||
/* Native messages no need to import the iris-ws.js to the ./templates.client.html
|
||||
Use of: OnMessage and EmitMessage.
|
||||
|
||||
|
||||
NOTICE: IF YOU HAVE RAN THE PREVIOUS EXAMPLES YOU HAVE TO CLEAR YOUR BROWSER's CACHE
|
||||
BECAUSE chat.js is different than the CACHED. OTHERWISE YOU WILL GET Ws is undefined from the browser's console, because it will use the cached.
|
||||
*/
|
||||
|
||||
type clientPage struct {
|
||||
Title string
|
||||
Host string
|
||||
@@ -26,36 +18,43 @@ func main() {
|
||||
|
||||
app.RegisterView(iris.HTML("./templates", ".html")) // select the html engine to serve templates
|
||||
|
||||
ws := websocket.New(websocket.Config{
|
||||
// to enable binary messages (useful for protobuf):
|
||||
// BinaryMessages: true,
|
||||
// Almost all features of neffos are disabled because no custom message can pass
|
||||
// when app expects to accept and send only raw websocket native messages.
|
||||
// When only allow native messages is a fact?
|
||||
// When the registered namespace is just one and it's empty
|
||||
// and contains only one registered event which is the `OnNativeMessage`.
|
||||
// When `Events{...}` is used instead of `Namespaces{ "namespaceName": Events{...}}`
|
||||
// then the namespace is empty "".
|
||||
ws := websocket.New(websocket.DefaultGorillaUpgrader, websocket.Events{
|
||||
websocket.OnNativeMessage: func(nsConn *websocket.NSConn, msg websocket.Message) error {
|
||||
log.Printf("Server got: %s from [%s]", msg.Body, nsConn.Conn.ID())
|
||||
|
||||
nsConn.Conn.Server().Broadcast(nsConn, msg)
|
||||
return nil
|
||||
},
|
||||
})
|
||||
|
||||
ws.OnConnect = func(c *websocket.Conn) error {
|
||||
log.Printf("[%s] Connected to server!", c.ID())
|
||||
return nil
|
||||
}
|
||||
|
||||
ws.OnDisconnect = func(c *websocket.Conn) {
|
||||
log.Printf("[%s] Disconnected from server", c.ID())
|
||||
}
|
||||
|
||||
app.HandleDir("/js", "./static/js") // serve our custom javascript code.
|
||||
|
||||
// register the server on an endpoint.
|
||||
// see the inline javascript code i the websockets.html, this endpoint is used to connect to the server.
|
||||
app.Get("/my_endpoint", ws.Handler())
|
||||
|
||||
app.HandleDir("/js", "./static/js") // serve our custom javascript code
|
||||
app.Get("/my_endpoint", websocket.Handler(ws))
|
||||
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
ctx.ViewData("", clientPage{"Client Page", "localhost:8080"})
|
||||
ctx.View("client.html")
|
||||
})
|
||||
|
||||
ws.OnConnection(func(c websocket.Connection) {
|
||||
|
||||
c.OnMessage(func(data []byte) {
|
||||
message := string(data)
|
||||
c.To(websocket.Broadcast).EmitMessage([]byte("Message from: " + c.ID() + "-> " + message)) // broadcast to all clients except this
|
||||
c.EmitMessage([]byte("Me: " + message)) // writes to itself
|
||||
})
|
||||
|
||||
c.OnDisconnect(func() {
|
||||
fmt.Printf("\nConnection with ID: %s has been disconnected!", c.ID())
|
||||
})
|
||||
|
||||
ctx.View("client.html", clientPage{"Client Page", "localhost:8080"})
|
||||
})
|
||||
|
||||
// Target some browser windows/tabs to http://localhost:8080 and send some messages,
|
||||
// see the static/js/chat.js,
|
||||
// note that the client is using only the browser's native WebSocket API instead of the neffos one.
|
||||
app.Run(iris.Addr(":8080"))
|
||||
|
||||
}
|
||||
|
||||
@@ -1,38 +1,35 @@
|
||||
var messageTxt;
|
||||
var messages;
|
||||
var messageTxt = document.getElementById("messageTxt");
|
||||
var messages = document.getElementById("messages");
|
||||
var sendBtn = document.getElementById("sendBtn")
|
||||
|
||||
$(function () {
|
||||
w = new WebSocket("ws://" + HOST + "/my_endpoint");
|
||||
w.onopen = function () {
|
||||
console.log("Websocket connection enstablished");
|
||||
};
|
||||
|
||||
messageTxt = $("#messageTxt");
|
||||
messages = $("#messages");
|
||||
w.onclose = function () {
|
||||
appendMessage("<div><center><h3>Disconnected</h3></center></div>");
|
||||
};
|
||||
w.onmessage = function (message) {
|
||||
appendMessage("<div>" + message.data + "</div>");
|
||||
};
|
||||
|
||||
sendBtn.onclick = function () {
|
||||
myText = messageTxt.value;
|
||||
messageTxt.value = "";
|
||||
|
||||
w = new WebSocket("ws://" + HOST + "/my_endpoint");
|
||||
w.onopen = function () {
|
||||
console.log("Websocket connection enstablished");
|
||||
};
|
||||
appendMessage("<div style='color: red'> me: " + myText + "</div>");
|
||||
w.send(myText);
|
||||
};
|
||||
|
||||
w.onclose = function () {
|
||||
appendMessage($("<div><center><h3>Disconnected</h3></center></div>"));
|
||||
};
|
||||
w.onmessage = function(message){
|
||||
appendMessage($("<div>" + message.data + "</div>"));
|
||||
};
|
||||
messageTxt.addEventListener("keyup", function (e) {
|
||||
if (e.keyCode === 13) {
|
||||
e.preventDefault();
|
||||
|
||||
sendBtn.click();
|
||||
}
|
||||
});
|
||||
|
||||
$("#sendBtn").click(function () {
|
||||
w.send(messageTxt.val().toString());
|
||||
messageTxt.val("");
|
||||
});
|
||||
|
||||
})
|
||||
|
||||
|
||||
function appendMessage(messageDiv) {
|
||||
var theDiv = messages[0];
|
||||
var doScroll = theDiv.scrollTop == theDiv.scrollHeight - theDiv.clientHeight;
|
||||
messageDiv.appendTo(messages);
|
||||
if (doScroll) {
|
||||
theDiv.scrollTop = theDiv.scrollHeight - theDiv.clientHeight;
|
||||
}
|
||||
function appendMessage(messageDivHTML) {
|
||||
messages.insertAdjacentHTML('afterbegin', messageDivHTML);
|
||||
}
|
||||
|
||||
@@ -1,21 +1,19 @@
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<title>{{ .Title}}</title>
|
||||
<title>{{ .Title}}</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div id="messages"
|
||||
style="border-width: 1px; border-style: solid; height: 400px; width: 375px;">
|
||||
|
||||
</div>
|
||||
<body style="padding:10px;">
|
||||
<input type="text" id="messageTxt" />
|
||||
<button type="button" id="sendBtn">Send</button>
|
||||
<div id="messages" style="width: 375px;margin:10 0 0 0px;border-top: 1px solid black;">
|
||||
</div>
|
||||
|
||||
<script type="text/javascript">
|
||||
var HOST = {{.Host}}
|
||||
var HOST = {{.Host }}
|
||||
</script>
|
||||
<script src="js/vendor/jquery-2.2.3.min.js" type="text/javascript"></script>
|
||||
<script src="js/chat.js" type="text/javascript"></script>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
</html>
|
||||
Reference in New Issue
Block a user