Example of use of gopherjs + websocket + jsonrpc
- Have Go 1.4 or better (
gopherjsrequirement) go get -u -v github.com/gopherjs/gopherjs[godoc]go get -u -v github.com/gopherjs/websocket[godoc]go get -u -v honnef.co/go/js/dom[godoc]
cd $GOROOT(or to any of the directories featured in your$GOROOTif you have more than one, like me)git clone https://github.com/dustywilson/gopherjs-rpc.gitcd gopherjs-rpccd wwwto enter thewwwdirectory where theclient.gofile is waiting for you to buildgopherjs build -m -o client.jsto build theclient.gofile intoclient.jscd ..to return to parentgo run server.goto run the server-side- Go to
http://localhost:5454/in your browser
If you got log messages in the browser dev console, it's working. The Output line is what the JavaScript
client is sending to the Go server. The Input line is what the Go server sends to the client in response.
The only thing that differs in each message is the timestamp. The server spits out the same info to the
console on its end as well.
Notice that both the backend and frontend share the file in the shared directory. In this case, they use
that to share the ChatMessage struct. For the most part, you can share code between frontend and backend.
You CAN'T use the core websocket lib with gopherjs. If you start getting errors saying something like
gopherjs doesn't support network then you've used the wrong package.
This is rpc on top of websocket. In this case, the web browser is hosting RPC for the server to call.
For as long as the client is connected to the server, the server is able to call on any of its RPC
functions. Totally backwards, but really cool that it's possible.