-
Notifications
You must be signed in to change notification settings - Fork 616
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #9: Add websocket support to demo clients
* Add websocket support to demo/server * Add simple websocket client in demo/wsclient
- Loading branch information
1 parent
872c191
commit 6102bfd
Showing
3 changed files
with
99 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
wsclient |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Package wsclient implements a simple web socket client | ||
// which reads lines from stdin and sends them to the | ||
// websocket url. | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"flag" | ||
"fmt" | ||
"log" | ||
"os" | ||
|
||
"github.com/eBay/fabio/_third_party/golang.org/x/net/websocket" | ||
) | ||
|
||
func main() { | ||
var url, origin string | ||
flag.StringVar(&url, "url", "ws://127.0.0.1:9999/echo", "websocket URL") | ||
flag.StringVar(&origin, "origin", "http://localhost/", "origin header") | ||
flag.Parse() | ||
|
||
if url == "" { | ||
flag.Usage() | ||
os.Exit(1) | ||
} | ||
|
||
ws, err := websocket.Dial(url, "", origin) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
go func() { | ||
var msg = make([]byte, 512) | ||
for { | ||
n, err := ws.Read(msg) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
fmt.Printf("R: %s\nS: ", msg[:n]) | ||
} | ||
}() | ||
|
||
fmt.Print("S: ") | ||
sc := bufio.NewScanner(os.Stdin) | ||
for sc.Scan() { | ||
if _, err := ws.Write(sc.Bytes()); err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
} |