mirror of
https://github.com/kataras/iris.git
synced 2025-02-02 15:30:36 +01:00
_examples/websocket/basic: add a nodejs client and provide a README.md on how to run the websocket clients and the server
Former-commit-id: a98a80996d7d95fa947e72c71803407682229fa7
This commit is contained in:
parent
5f148987f7
commit
272566950d
5
.gitignore
vendored
5
.gitignore
vendored
|
@ -1,4 +1,5 @@
|
|||
.vscode
|
||||
_authortools
|
||||
/_examples/**/node_modules
|
||||
.directory
|
||||
.directory
|
||||
node_modules
|
||||
package-lock.json
|
62
_examples/websocket/basic/README.md
Normal file
62
_examples/websocket/basic/README.md
Normal file
|
@ -0,0 +1,62 @@
|
|||
# Basic Example
|
||||
|
||||
At the end of this example you will be able to run a websocket server
|
||||
and clients for all platforms (Go, Browser and Nodejs).
|
||||
|
||||
![](overview.png)
|
||||
|
||||
Open as many clients as you want to try out and start typing.
|
||||
|
||||
This example contains only the basics, however, the library supports rooms, native websocket messages, any data can be sent and received (i.e protobufs, json) and all kinds of broadcasting and connections collections.
|
||||
|
||||
## How to run
|
||||
|
||||
### Server
|
||||
|
||||
Open a terminal window instance and execute:
|
||||
|
||||
```sh
|
||||
$ go run server.go # start the websocket server.
|
||||
```
|
||||
|
||||
### Client (Go)
|
||||
|
||||
Start a new terminal instance and execute:
|
||||
|
||||
```sh
|
||||
$ cd ./go-client
|
||||
$ go run client.go # start the websocket client.
|
||||
# start typing...
|
||||
```
|
||||
|
||||
### Client (Browser)
|
||||
|
||||
Navigate to <http://localhost:8080> and start typing.
|
||||
The `./browser/index.html` should be served, it contains the client-side code.
|
||||
|
||||
### Client (Browserify)
|
||||
|
||||
Install [NPM](https://nodejs.org) first, then start a new terminal instance and execute:
|
||||
|
||||
```sh
|
||||
$ cd ./browserify
|
||||
$ npm install
|
||||
# build the modern browser-side client:
|
||||
# embed the neffos.js node-module and app.js
|
||||
# into a single ./browserify/bundle.js file
|
||||
# which ./browserify/client.html imports.
|
||||
$ npm run-script build
|
||||
```
|
||||
|
||||
Navigate to <http://localhost:8080/browserify/client.html> and start typing.
|
||||
|
||||
### Client (Nodejs)
|
||||
|
||||
Install [NPM](https://nodejs.org) if you haven't already and then, start a new terminal instance and execute:
|
||||
|
||||
```sh
|
||||
$ cd nodejs-client
|
||||
$ npm install
|
||||
$ node client.js # start the websocket client.
|
||||
# start typing.
|
||||
```
|
|
@ -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.8/dist/neffos.min.js"></script>
|
||||
<script src="https://cdn.jsdelivr.net/npm/neffos.js@0.1.12/dist/neffos.min.js"></script>
|
||||
<script>
|
||||
// `neffos` global variable is available now.
|
||||
var scheme = document.location.protocol == "https:" ? "wss" : "ws";
|
||||
|
@ -28,6 +28,8 @@
|
|||
}
|
||||
|
||||
function handleNamespaceConnectedConn(nsConn) {
|
||||
nsConn.emit("Hello from browser client side!");
|
||||
|
||||
let inputTxt = document.getElementById("input");
|
||||
let sendBtn = document.getElementById("sendBtn");
|
||||
|
||||
|
@ -62,7 +64,7 @@
|
|||
// You can either wait to conenct or just conn.connect("connect")
|
||||
// and put the `handleNamespaceConnectedConn` inside `_OnNamespaceConnected` callback instead.
|
||||
// const nsConn = await conn.connect("default");
|
||||
// handleNamespaceConnectedConn(nsConn);
|
||||
// nsConn.emit(...); handleNamespaceConnectedConn(nsConn);
|
||||
conn.connect("default");
|
||||
|
||||
} catch (err) {
|
||||
|
|
|
@ -1,11 +0,0 @@
|
|||
# Browserify example
|
||||
|
||||
```sh
|
||||
$ npm install --only=dev # install browserify from the devDependencies.
|
||||
$ npm run-script build # browserify and minify the `app.js` into `bundle.js`.
|
||||
$ cd ../ && go run server.go # start the neffos server.
|
||||
```
|
||||
|
||||
> make sure that you have [golang](https://golang.org/dl) installed to run and edit the neffos (server-side).
|
||||
|
||||
That's all, now navigate to <http://localhost:8080/browserify>.
|
|
@ -6,6 +6,7 @@ var port = document.location.port ? ":" + document.location.port : "";
|
|||
var wsURL = scheme + "://" + document.location.hostname + port + "/echo";
|
||||
|
||||
var outputTxt = document.getElementById("output");
|
||||
|
||||
function addMessage(msg) {
|
||||
outputTxt.innerHTML += msg + "\n";
|
||||
}
|
||||
|
@ -16,6 +17,8 @@ function handleError(reason) {
|
|||
}
|
||||
|
||||
function handleNamespaceConnectedConn(nsConn) {
|
||||
nsConn.emit("chat", "Hello from browser(ify) client-side!");
|
||||
|
||||
const inputTxt = document.getElementById("input");
|
||||
const sendBtn = document.getElementById("sendBtn");
|
||||
|
||||
|
@ -50,6 +53,7 @@ async function runExample() {
|
|||
// and put the `handleNamespaceConnectedConn` inside `_OnNamespaceConnected` callback instead.
|
||||
// const nsConn = await conn.connect("default");
|
||||
// handleNamespaceConnectedConn(nsConn);
|
||||
// nsConn.emit(...); handleNamespaceConnectedConn(nsConn);
|
||||
conn.connect("default");
|
||||
|
||||
} catch (err) {
|
||||
|
@ -57,5 +61,4 @@ async function runExample() {
|
|||
}
|
||||
}
|
||||
|
||||
runExample();
|
||||
|
||||
runExample();
|
File diff suppressed because one or more lines are too long
|
@ -24,20 +24,15 @@ const (
|
|||
var clientEvents = websocket.Namespaces{
|
||||
namespace: websocket.Events{
|
||||
websocket.OnNamespaceConnected: func(c *websocket.NSConn, msg websocket.Message) error {
|
||||
log.Printf("[%s] connected to namespace [%s]", c, msg.Namespace)
|
||||
log.Printf("connected to namespace: %s", msg.Namespace)
|
||||
return nil
|
||||
},
|
||||
websocket.OnNamespaceDisconnect: func(c *websocket.NSConn, msg websocket.Message) error {
|
||||
log.Printf("[%s] disconnected from namespace [%s]", c, msg.Namespace)
|
||||
log.Printf("disconnected from namespace: %s", msg.Namespace)
|
||||
return nil
|
||||
},
|
||||
"chat": func(c *websocket.NSConn, msg websocket.Message) error {
|
||||
log.Printf("[%s] sent: %s", c.Conn.ID(), string(msg.Body))
|
||||
|
||||
// Write message back to the client message owner with:
|
||||
// c.Emit("chat", msg)
|
||||
// Write message to all except this client with:
|
||||
c.Conn.Server().Broadcast(c, msg)
|
||||
log.Printf("%s", string(msg.Body))
|
||||
return nil
|
||||
},
|
||||
},
|
||||
|
@ -58,6 +53,8 @@ func main() {
|
|||
panic(err)
|
||||
}
|
||||
|
||||
c.Emit("chat", []byte("Hello from Go client side!"))
|
||||
|
||||
fmt.Fprint(os.Stdout, ">> ")
|
||||
scanner := bufio.NewScanner(os.Stdin)
|
||||
for {
|
||||
|
|
35
_examples/websocket/basic/nodejs-client/client.js
Normal file
35
_examples/websocket/basic/nodejs-client/client.js
Normal file
|
@ -0,0 +1,35 @@
|
|||
const neffos = require('neffos.js');
|
||||
const stdin = process.openStdin();
|
||||
|
||||
const wsURL = "ws://localhost:8080/echo";
|
||||
|
||||
async function runExample() {
|
||||
try {
|
||||
const conn = await neffos.dial(wsURL, {
|
||||
default: { // "default" namespace.
|
||||
_OnNamespaceConnected: function (nsConn, msg) {
|
||||
console.log("connected to namespace: " + msg.Namespace);
|
||||
},
|
||||
_OnNamespaceDisconnect: function (nsConn, msg) {
|
||||
console.log("disconnected from namespace: " + msg.Namespace);
|
||||
},
|
||||
chat: function (nsConn, msg) { // "chat" event.
|
||||
console.log(msg.Body);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
const nsConn = await conn.connect("default");
|
||||
nsConn.emit("chat", "Hello from Nodejs client side!");
|
||||
|
||||
stdin.addListener("data", function (data) {
|
||||
const text = data.toString().trim();
|
||||
nsConn.emit("chat", text);
|
||||
});
|
||||
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
}
|
||||
}
|
||||
|
||||
runExample();
|
8
_examples/websocket/basic/nodejs-client/package.json
Normal file
8
_examples/websocket/basic/nodejs-client/package.json
Normal file
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"name": "neffos.js.example.nodejsclient",
|
||||
"version": "0.0.1",
|
||||
"main": "client.js",
|
||||
"dependencies": {
|
||||
"neffos.js": "latest"
|
||||
}
|
||||
}
|
BIN
_examples/websocket/basic/overview.png
Normal file
BIN
_examples/websocket/basic/overview.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 202 KiB |
|
@ -40,7 +40,7 @@ var serverEvents = websocket.Namespaces{
|
|||
func main() {
|
||||
app := iris.New()
|
||||
websocketServer := websocket.New(
|
||||
websocket.DefaultGorillaUpgrader, /*DefaultGobwasUpgrader can be used as well*/
|
||||
websocket.DefaultGorillaUpgrader, /* DefaultGobwasUpgrader can be used too. */
|
||||
serverEvents)
|
||||
|
||||
// serves the endpoint of ws://localhost:8080/echo
|
||||
|
@ -54,5 +54,5 @@ func main() {
|
|||
// serves the npm browser websocket client usage example.
|
||||
app.StaticWeb("/browserify", "./browserify")
|
||||
|
||||
app.Run(iris.Addr(":8080"))
|
||||
app.Run(iris.Addr(":8080"), iris.WithoutServerError(iris.ErrServerClosed))
|
||||
}
|
||||
|
|
2
go.mod
2
go.mod
|
@ -15,7 +15,7 @@ require (
|
|||
github.com/iris-contrib/go.uuid v2.0.0+incompatible
|
||||
github.com/json-iterator/go v1.1.6
|
||||
github.com/kataras/golog v0.0.0-20180321173939-03be10146386
|
||||
github.com/kataras/neffos v0.0.0-20190606200227-c8dd500b4cdf
|
||||
github.com/kataras/neffos v0.0.1
|
||||
github.com/kataras/pio v0.0.0-20190103105442-ea782b38602d // indirect
|
||||
github.com/microcosm-cc/bluemonday v1.0.2
|
||||
github.com/ryanuber/columnize v2.1.0+incompatible
|
||||
|
|
63
go.sum
63
go.sum
|
@ -1,63 +0,0 @@
|
|||
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
|
||||
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
||||
github.com/Joker/hpp v0.0.0-20180418125244-6893e659854a/go.mod h1:MzD2WMdSxvbHw5fM/OXOFily/lipJWRc9C1px0Mt0ZE=
|
||||
github.com/Joker/jade v1.0.0 h1:lOCEPvTAtWfLpSZYMOv/g44MGQFAolbKh2khHHGu0Kc=
|
||||
github.com/Joker/jade v1.0.0/go.mod h1:efZIdO0py/LtcJRSa/j2WEklMSAw84WV0zZVMxNToB8=
|
||||
github.com/Shopify/goreferrer v0.0.0-20181106222321-ec9c9a553398 h1:WDC6ySpJzbxGWFh4aMxFFC28wwGp5pEuoTtvA4q/qQ4=
|
||||
github.com/Shopify/goreferrer v0.0.0-20181106222321-ec9c9a553398/go.mod h1:a1uqRtAwp2Xwc6WNPJEufxJ7fx3npB4UV/JOLmbu5I0=
|
||||
github.com/aymerick/raymond v2.0.2+incompatible h1:VEp3GpgdAnv9B2GFyTvqgcKvY+mfKMjPOA3SbKLtnU0=
|
||||
github.com/aymerick/raymond v2.0.2+incompatible/go.mod h1:osfaiScAUVup+UC9Nfq76eWqDhXlp+4UYaA8uhTBO6g=
|
||||
github.com/eknkc/amber v0.0.0-20171010120322-cdade1c07385 h1:clC1lXBpe2kTj2VHdaIu9ajZQe4kcEY9j0NsnDDBZ3o=
|
||||
github.com/eknkc/amber v0.0.0-20171010120322-cdade1c07385/go.mod h1:0vRUJqYpeSZifjYj7uP3BG/gKcuzL9xWVV/Y+cK33KM=
|
||||
github.com/fatih/structs v1.1.0 h1:Q7juDM0QtcnhCpeyLGQKyg4TOIghuNXrkL32pHAUMxo=
|
||||
github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M=
|
||||
github.com/flosch/pongo2 v0.0.0-20190505152737-8914e1cf9164 h1:/HMcOGZC5Bi8JPgfbwz13ELWn/91+vY59HXS3z0qY5w=
|
||||
github.com/flosch/pongo2 v0.0.0-20190505152737-8914e1cf9164/go.mod h1:tbAXHifHQWNSpWbiJHpJTZH5fi3XHhDMdP//vuz9WS4=
|
||||
github.com/gobwas/httphead v0.0.0-20180130184737-2c6c146eadee/go.mod h1:L0fX3K22YWvt/FAX9NnzrNzcI4wNYi9Yku4O0LKYflo=
|
||||
github.com/gobwas/pool v0.2.0/go.mod h1:q8bcK0KcYlCgd9e7WYLm9LpyS+YeLd8JVDW6WezmKEw=
|
||||
github.com/gobwas/ws v1.0.1/go.mod h1:szmBTxLgaFppYjEmNtny/v3w89xOydFnnZMcgRRu/EM=
|
||||
github.com/gorilla/websocket v1.4.0 h1:WDFjx/TMzVgy9VdMMQi2K2Emtwi2QcUQsztZ/zLaH/Q=
|
||||
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
|
||||
github.com/iris-contrib/blackfriday v2.0.0+incompatible h1:o5sHQHHm0ToHUlAJSTjW9UWicjJSDDauOOQ2AHuIVp4=
|
||||
github.com/iris-contrib/blackfriday v2.0.0+incompatible/go.mod h1:UzZ2bDEoaSGPbkg6SAB4att1aAwTmVIx/5gCVqeyUdI=
|
||||
github.com/iris-contrib/formBinder v0.0.0-20190104093907-fbd5963f41e1 h1:7GsNnSLoVceNylMpwcfy5aFNz/S5/TV25crb34I5PEo=
|
||||
github.com/iris-contrib/formBinder v0.0.0-20190104093907-fbd5963f41e1/go.mod h1:i8kTYUOEstd/S8TG0ChTXQdf4ermA/e8vJX0+QruD9w=
|
||||
github.com/iris-contrib/go.uuid v2.0.0+incompatible h1:XZubAYg61/JwnJNbZilGjf3b3pB80+OQg2qf6c8BfWE=
|
||||
github.com/iris-contrib/go.uuid v2.0.0+incompatible/go.mod h1:iz2lgM/1UnEf1kP0L/+fafWORmlnuysV2EMP8MW+qe0=
|
||||
github.com/json-iterator/go v1.1.6 h1:MrUvLMLTMxbqFJ9kzlvat/rYZqZnW3u4wkLzWTaFwKs=
|
||||
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
|
||||
github.com/kataras/golog v0.0.0-20180321173939-03be10146386 h1:VT6AeCHO/mc+VedKBMhoqb5eAK8B1i9F6nZl7EGlHvA=
|
||||
github.com/kataras/golog v0.0.0-20180321173939-03be10146386/go.mod h1:PcaEvfvhGsqwXZ6S3CgCbmjcp+4UDUh2MIfF2ZEul8M=
|
||||
github.com/kataras/neffos v0.0.0-20190606200227-c8dd500b4cdf h1:rHFU6nupBNP5RejMwghFnhjUoOFXAYxmE07s4f5aeXQ=
|
||||
github.com/kataras/neffos v0.0.0-20190606200227-c8dd500b4cdf/go.mod h1:XfxmcgJUtbPmzK9wPLE7ybFHXoCqZKGptaW1frrxFhw=
|
||||
github.com/kataras/pio v0.0.0-20190103105442-ea782b38602d h1:V5Rs9ztEWdp58oayPq/ulmlqJJZeJP6pP79uP3qjcao=
|
||||
github.com/kataras/pio v0.0.0-20190103105442-ea782b38602d/go.mod h1:NV88laa9UiiDuX9AhMbDPkGYSPugBOV6yTZB1l2K9Z0=
|
||||
github.com/microcosm-cc/bluemonday v1.0.2 h1:5lPfLTTAvAbtS0VqT+94yOtFnGfUWYyx0+iToC3Os3s=
|
||||
github.com/microcosm-cc/bluemonday v1.0.2/go.mod h1:iVP4YcDBq+n/5fb23BhYFvIMq/leAFZyRl6bYmGDlGc=
|
||||
github.com/ryanuber/columnize v2.1.0+incompatible h1:j1Wcmh8OrK4Q7GXY+V7SVSY8nUWQxHW5TkBe7YUl+2s=
|
||||
github.com/ryanuber/columnize v2.1.0+incompatible/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
|
||||
github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
|
||||
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20190530122614-20be4c3c3ed5 h1:8dUaAV7K4uHsF56JQWkprecIQKdPHtR9jCHF5nB8uzc=
|
||||
golang.org/x/crypto v0.0.0-20190530122614-20be4c3c3ed5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||
golang.org/x/net v0.0.0-20181023162649-9b4f9f5ad519/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20181220203305-927f97764cc3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3 h1:0GoQqolDA55aaLxZyTzK/Y2ePZzZTUrRacwib7cNsYQ=
|
||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/net v0.0.0-20190522155817-f3200d17e092 h1:4QSRKanuywn15aTZvI/mIDEgPQpswuFndXpOj3rKEco=
|
||||
golang.org/x/net v0.0.0-20190522155817-f3200d17e092/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20190602015325-4c4f7f33c9ed/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
|
||||
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20181221001348-537d06c36207/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20190602112858-2de7f9bf822c/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
|
||||
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
|
||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
Loading…
Reference in New Issue
Block a user