Explorar o código

go.net/websocket: fix server and client examples

Fix import path, and remove some unnecessary semicolons.

R=golang-dev, dave, adg
CC=golang-dev
https://golang.org/cl/6506068
Francisco Souza %!s(int64=13) %!d(string=hai) anos
pai
achega
3b94eae23e
Modificáronse 4 ficheiros con 59 adicións e 57 borrados
  1. 1 31
      websocket/client.go
  2. 31 0
      websocket/exampledial_test.go
  3. 26 0
      websocket/examplehandler_test.go
  4. 1 26
      websocket/server.go

+ 1 - 31
websocket/client.go

@@ -64,37 +64,7 @@ func NewClient(config *Config, rwc io.ReadWriteCloser) (ws *Conn, err error) {
 	return
 }
 
-/*
-Dial opens a new client connection to a WebSocket.
-
-A trivial example client:
-
-	package main
-
-	import (
-		"log"
-		"net/http"
-		"strings"
-		"websocket"
-	)
-
-	func main() {
-		origin := "http://localhost/"
-		url := "ws://localhost/ws" 
-		ws, err := websocket.Dial(url, "", origin)
-		if err != nil {
-			log.Fatal(err)
-		}
-		if _, err := ws.Write([]byte("hello, world!\n")); err != nil {
-			log.Fatal(err)
-		}
-		var msg = make([]byte, 512);
-		if n, err := ws.Read(msg); err != nil {
-			log.Fatal(err)
-		}
-		// use msg[0:n]
-	}
-*/
+// Dial opens a new client connection to a WebSocket.
 func Dial(url_, protocol, origin string) (ws *Conn, err error) {
 	config, err := NewConfig(url_, origin)
 	if err != nil {

+ 31 - 0
websocket/exampledial_test.go

@@ -0,0 +1,31 @@
+// Copyright 2012 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package websocket_test
+
+import (
+	"fmt"
+	"log"
+
+	"code.google.com/p/go.net/websocket"
+)
+
+// This example demonstrates a trivial client.
+func ExampleDial() {
+	origin := "http://localhost/"
+	url := "ws://localhost:12345/ws"
+	ws, err := websocket.Dial(url, "", origin)
+	if err != nil {
+		log.Fatal(err)
+	}
+	if _, err := ws.Write([]byte("hello, world!\n")); err != nil {
+		log.Fatal(err)
+	}
+	var msg = make([]byte, 512)
+	var n int
+	if n, err = ws.Read(msg); err != nil {
+		log.Fatal(err)
+	}
+	fmt.Printf("Received: %s.\n", msg[:n])
+}

+ 26 - 0
websocket/examplehandler_test.go

@@ -0,0 +1,26 @@
+// Copyright 2012 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package websocket_test
+
+import (
+	"io"
+	"net/http"
+
+	"code.google.com/p/go.net/websocket"
+)
+
+// Echo the data received on the WebSocket.
+func EchoServer(ws *websocket.Conn) {
+	io.Copy(ws, ws)
+}
+
+// This example demonstrates a trivial echo server.
+func ExampleHandler() {
+	http.Handle("/echo", websocket.Handler(EchoServer))
+	err := http.ListenAndServe(":12345", nil)
+	if err != nil {
+		panic("ListenAndServe: " + err.Error())
+	}
+}

+ 1 - 26
websocket/server.go

@@ -52,32 +52,7 @@ func newServerConn(rwc io.ReadWriteCloser, buf *bufio.ReadWriter, req *http.Requ
 	return
 }
 
-/*
-Handler is an interface to a WebSocket.
-
-A trivial example server:
-
-	package main
-
-	import (
-		"io"
-		"net/http"
-		"websocket"
-	)
-
-	// Echo the data received on the WebSocket.
-	func EchoServer(ws *websocket.Conn) {
-		io.Copy(ws, ws);
-	}
-
-	func main() {
-		http.Handle("/echo", websocket.Handler(EchoServer));
-		err := http.ListenAndServe(":12345", nil);
-		if err != nil {
-			panic("ListenAndServe: " + err.Error())
-		}
-	}
-*/
+// Handler is an interface to a WebSocket.
 type Handler func(*Conn)
 
 // ServeHTTP implements the http.Handler interface for a Web Socket