Golang(10)Web Service - Web Socket

Golang(10)Web Service - Web Socket

8.1 Socket
2 types of socket, TCP Socket, UDP Socket.  IP, port, TCP/UDP.

8.2 WebSocket
Implement the Push Notification to Browsers
https://github.com/astaxie/build-web-application-with-golang/blob/master/ebook/08.2.md

iOS and Android can support web socket too.
http://www.elabs.se/blog/66-using-websockets-in-native-ios-and-android-apps

Install the the go.net package
>go get code.google.com/p/go.net/websocket

Error Message:
go: missing Mercurial command. See http://golang.org/s/gogetcmd package code.google.com/p/go.net/websocket: exec: "hg": executable file not found in $PATH

Solution:
https://code.google.com/p/go-wiki/wiki/GoGetTools
>sudo port install mercurial

Error Message:
Error: org.macports.extract for port py27-docutils returned: command execution failed Error: Failed to install py27-docutils
:error:extract Failed to install py27-docutils :debug:extract couldn't open "/System/Library/Frameworks/Tcl.framework/Versions/8.5/Resources/tclIndex": no such file or directory    while executing

Solution:
>sudo port clean
>sudo port selfupdate
>sudo port install py27-docutils
>sudo port install mercurial

>>go get code.google.com/p/go.net/websocket

Follow the document, but I get these error when I try to connect to web socket server.

Error Message:
failed: Error during WebSocket handshake: Unexpected response code: 403

Solution:
Make them the same domain name.

Go Server Side
package main

import (
     "code.google.com/p/go.net/websocket"
     "fmt"
     "log"
     "net/http"
)

func Echo(ws *websocket.Conn) {
     var err error

     for {
          var reply string

          if err = websocket.Message.Receive(ws, &reply); err != nil {
               fmt.Println("Can't receive")
               break
          }

          fmt.Println("Received back from client: " + reply)

          msg := "Received:  " + reply
          fmt.Println("Sending to client: " + msg)

          if err = websocket.Message.Send(ws, msg); err != nil {
               fmt.Println("Can't send")
               break
          }
     }
}

func main() {
     http.Handle("/", http.FileServer(http.Dir(".")))
     http.Handle("/socket", websocket.Handler(Echo))

     if err := http.ListenAndServe("127.0.0.1:8008", nil); err != nil {
          log.Fatal("ListenAndServe:", err)
     }
}

HTML Client Side
<html>
<head></head>
<body>
    <script type="text/javascript">
        var sock = null;
        var wsuri = "ws://127.0.0.1:8008/socket";

        window.onload = function() {

            console.log("onload");

            sock = new WebSocket(wsuri);

            sock.onopen = function() {
                console.log("connected to " + wsuri);
            }

            sock.onclose = function(e) {
                console.log("connection closed (" + e.code + ")");
            }

            sock.onmessage = function(e) {
                console.log("message received: " + e.data);
            }
        };

        function send() {
            var msg = document.getElementById('message').value;
            sock.send(msg);
        };
    </script>
    <h1>WebSocket Echo Test</h1>
    <form>
        <p>
            Message: <input id="message" type="text" value="Hello, world!">
        </p>
    </form>
    <button onclick="send();">Send Message</button>
</body>
</html>


8.3 REST
…soon...

8.4 RCP (Remote Procedure Call Protocol)
…snip...

References:
https://github.com/astaxie/build-web-application-with-golang/blob/master/ebook/08.0.md

web socket push notification
http://html5hacks.com/blog/2013/04/21/push-notifications-to-the-browser-with-server-sent-events/
http://stackoverflow.com/questions/15884480/static-html-page-created-the-websocket-connection-golang-server-directly


猜你喜欢

转载自sillycat.iteye.com/blog/2068488