This greeter server is a REST server that transparently supports the Websocket protocol. This Websocket binding is originated from Atmosphere’s websocket binding [1], which in turn is originated from SwaggerSocket[2].
This binding can be considered as a simplified yet more efficient and flexible version of its predecessors.
The server side code has been generated from the included swagger.yaml using go swagger by command
$ swagger generate server -f swagger.yaml -A greeter-demo
The greeter service provides the following operations.
ping - ping the service echo - echo back the message greet - greet one specific person or everyone status - return the greeting status summary - return the greeting summary subscribe - subscribe to the greeting channel to be greeted unsubscribe - unsubscribe from the greeting channel
The last two operations take advantage of Websocket and the subscribed client can receive messages when someone greets the user associated with the client.
To start this server at http port 8091, run
$ go run cmd/greeter-demo-server/main.go --scheme http --port 8091
Or to start this server at https port 8495, run
$ go run cmd/greeter-demo-server/main.go --scheme https --tls-port 8495 --tls-certificate certs/localhost.crt --tls-key certs/localhost.key
$ go run cmd/greeter-demo-server/main.go --port 8091 --tls-port 8495 --tls-certificate certs/localhost.crt --tls-key certs/localhost.key 2018/04/07 09:26:47 Serving greeter demo at http://127.0.0.1:8091 2018/04/07 09:26:47 Serving greeter demo at https://127.0.0.1:8495
and open the page http://localhost:8091/swagger.json to view its swagger document.
To try out its Websocket protocol support, run the node client located in examples/clients/node-client.
To start this client against the above locally started greeter service, run
$ npm install $ node client.js
$ node client.js Client ID: e1f9cd89-6eb5-4d6e-b799-e318bf6ce20c Host URL: http://localhost:8091/samples/greeter (No Authentication) Choose user ... user: elakito Connecting using websocket ... elakito> Connected using websocket elakito> ping elakito> res0: {"code":200,"id":"0","type":"application/json"}{"pong":2} elakito> echo buenos dias elakito> res1: {"code":200,"id":"1","type":"application/json"}{"echo":"buenos dias"} elakito> subscribe elakito> res2: {"code":200,"id":"2","type":"application/json"}[] elakito> greet estrella hola estrella elakito> res3: {"code":200,"id":"3","type":"application/json"}{"from":"elakito","name":"estrella","text":"hola estrella"} elakito> res2: {"code":200,"id":"2","type":"application/json"}{"from":"estrella","name":"elakito","text":"hola elakito!"} elakito> res2: {"code":200,"id":"2","type":"application/json"}{"from":"estrella","name":"*","text":"hola a todos!!"} elakito> status elakito elakito> res4: {"code":200,"id":"4","type":"application/json"}{"count":2,"name":"elakito"} elakito> status estrella elakito> res5: {"code":200,"id":"5","type":"application/json"}{"count":4,"name":"estrella"} elakito> summary elakito> res6: {"code":200,"id":"6","type":"application/json"}{"greeted":["elakito","estrella"],"total":6} elakito>
$ node client.js Client ID: e222aa65-21a9-49a2-ab68-0bdae8c277d7 Host URL: http://localhost:8091/samples/greeter (No Authentication) Choose user ... user: estrella Connecting using websocket ... estrella> Connected using websocket estrella> subscribe estrella> res0: {"code":200,"id":"0","type":"application/json"}[] estrella> res0: {"code":200,"id":"0","type":"application/json"}{"from":"elakito","name":"estrella","text":"hola estrella"} estrella> greet elakito hola elakito! estrella> res1: {"code":200,"id":"1","type":"application/json"}{"from":"estrella","name":"elakito","text":"hola elakito!"} estrella> greet * hola a todos!! estrella> res0: {"code":200,"id":"0","type":"application/json"}{"from":"estrella","name":"*","text":"hola a todos!!"} estrella> res2: {"code":200,"id":"2","type":"application/json"}{"from":"estrella","name":"*","text":"hola a todos!!"} estrella> unsubscribe 0 estrella> res3: {"code":200,"id":"3","type":"application/json"}{"greeted":["elakito","estrella"],"total":6} estrella>