-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #111 from lesismal/http_conn_pool
websocket client: write with mask
- Loading branch information
Showing
6 changed files
with
282 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"time" | ||
|
||
"github.com/gorilla/websocket" | ||
) | ||
|
||
var ( | ||
proxyServerAddr = "ws://localhost:8888/ws" | ||
) | ||
|
||
func main() { | ||
c, _, err := websocket.DefaultDialer.Dial(proxyServerAddr, nil) | ||
if err != nil { | ||
log.Fatalf("Dial failed: %v, %v", proxyServerAddr, err) | ||
} | ||
defer c.Close() | ||
|
||
for i := 0; i < 10; i++ { | ||
{ | ||
request := fmt.Sprintf("hello %v", i) | ||
err := c.WriteMessage(websocket.BinaryMessage, []byte(request)) | ||
if err != nil { | ||
log.Fatalf("write: %v", err) | ||
return | ||
} | ||
|
||
receiveType, response, err := c.ReadMessage() | ||
if err != nil { | ||
log.Println("ReadMessage failed:", err) | ||
return | ||
} | ||
if receiveType != websocket.BinaryMessage { | ||
log.Printf("received type(%d) != websocket.BinaryMessage(%d)\n", receiveType, websocket.BinaryMessage) | ||
return | ||
|
||
} | ||
|
||
if string(response) != request { | ||
log.Printf("'%v' != '%v'", len(response), len(request)) | ||
return | ||
} | ||
|
||
log.Printf("success echo: [websocket.BinaryMessage], %v", request) | ||
} | ||
|
||
{ | ||
request := fmt.Sprintf("hello %v", i) | ||
err := c.WriteMessage(websocket.TextMessage, []byte(request)) | ||
if err != nil { | ||
log.Fatalf("write: %v", err) | ||
return | ||
} | ||
|
||
receiveType, response, err := c.ReadMessage() | ||
if err != nil { | ||
log.Println("ReadMessage failed:", err) | ||
return | ||
} | ||
if receiveType != websocket.TextMessage { | ||
log.Printf("received type(%d) != websocket.TextMessage(%d)\n", receiveType, websocket.TextMessage) | ||
return | ||
|
||
} | ||
|
||
if string(response) != request { | ||
log.Printf("'%v' != '%v'", len(response), len(request)) | ||
return | ||
} | ||
|
||
log.Printf("success echo: [websocket.TextMessage], %v", request) | ||
} | ||
time.Sleep(time.Second) | ||
} | ||
} |
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,58 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"net/http" | ||
|
||
"github.com/gorilla/websocket" | ||
) | ||
|
||
func msgType(messageType int) string { | ||
switch messageType { | ||
case websocket.BinaryMessage: | ||
return "[websocket.BinaryMessage]" | ||
case websocket.TextMessage: | ||
return "[websocket.TextMessage]" | ||
default: | ||
} | ||
return "[]" | ||
} | ||
|
||
func echo(w http.ResponseWriter, r *http.Request) { | ||
upgrader := &websocket.Upgrader{} | ||
c, err := upgrader.Upgrade(w, r, nil) | ||
if err != nil { | ||
log.Print("upgrade:", err) | ||
return | ||
} | ||
defer c.Close() | ||
for { | ||
messageType, message, err := c.ReadMessage() | ||
if err != nil { | ||
log.Println("read failed:", err) | ||
break | ||
} | ||
|
||
log.Printf("app server onMessage: %v, %v", msgType(messageType), string(message)) | ||
|
||
err = c.WriteMessage(messageType, message) | ||
if err != nil { | ||
log.Println("write failed:", err) | ||
break | ||
} | ||
} | ||
} | ||
|
||
var ( | ||
appServerAddr = "localhost:9999" | ||
) | ||
|
||
func main() { | ||
mux := &http.ServeMux{} | ||
mux.HandleFunc("/ws", echo) | ||
server := http.Server{ | ||
Addr: appServerAddr, | ||
Handler: mux, | ||
} | ||
log.Println("server exit:", server.ListenAndServe()) | ||
} |
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,105 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"os" | ||
"os/signal" | ||
"time" | ||
|
||
"github.com/lesismal/nbio/nbhttp" | ||
"github.com/lesismal/nbio/nbhttp/websocket" | ||
) | ||
|
||
var ( | ||
proxyServerAddr = "localhost:8888" | ||
|
||
appServerAddr = "ws://localhost:9999/ws" | ||
|
||
proxyServer *nbhttp.Server | ||
) | ||
|
||
func msgType(messageType websocket.MessageType) string { | ||
switch messageType { | ||
case websocket.BinaryMessage: | ||
return "[websocket.BinaryMessage]" | ||
case websocket.TextMessage: | ||
return "[websocket.TextMessage]" | ||
default: | ||
} | ||
return "[]" | ||
} | ||
|
||
func newUpgrader() *websocket.Upgrader { | ||
u := websocket.NewUpgrader() | ||
u.OnMessage(func(c *websocket.Conn, messageType websocket.MessageType, data []byte) { | ||
peer, ok := c.Session().(*websocket.Conn) | ||
if ok { | ||
log.Printf("proxy server onMessage [%v -> %v]: %v, %v", c.RemoteAddr(), peer.RemoteAddr(), msgType(messageType), string(data)) | ||
peer.WriteMessage(messageType, data) | ||
} | ||
}) | ||
u.OnClose(func(c *websocket.Conn, err error) { | ||
peer, ok := c.Session().(*websocket.Conn) | ||
if ok { | ||
log.Printf("proxy server onClose [%v -> %v]", c.RemoteAddr(), peer.RemoteAddr()) | ||
peer.Close() | ||
} else { | ||
log.Printf("proxy server onClose [%v -> %v]", c.RemoteAddr(), "") | ||
} | ||
}) | ||
return u | ||
} | ||
|
||
func onWebsocket(w http.ResponseWriter, r *http.Request) { | ||
upgrader := newUpgrader() | ||
conn, err := upgrader.Upgrade(w, r, nil) | ||
if err != nil { | ||
panic(err) | ||
} | ||
srcConn := conn.(*websocket.Conn) | ||
|
||
dialer := &websocket.Dialer{ | ||
Engine: proxyServer.Engine, | ||
Upgrader: newUpgrader(), | ||
DialTimeout: time.Second * 3, | ||
} | ||
dstConn, _, err := dialer.Dial(appServerAddr, nil) | ||
if err != nil { | ||
log.Printf("Dial failed: %v, %v", appServerAddr, err) | ||
srcConn.Close() | ||
return | ||
} | ||
|
||
srcConn.SetSession(dstConn) | ||
dstConn.SetSession(srcConn) | ||
} | ||
|
||
func main() { | ||
flag.Parse() | ||
mux := &http.ServeMux{} | ||
mux.HandleFunc("/ws", onWebsocket) | ||
|
||
proxyServer = nbhttp.NewServer(nbhttp.Config{ | ||
Network: "tcp", | ||
Addrs: []string{proxyServerAddr}, | ||
|
||
SupportClient: true, | ||
}, mux, nil) | ||
|
||
err := proxyServer.Start() | ||
if err != nil { | ||
fmt.Printf("nbio.Start failed: %v\n", err) | ||
return | ||
} | ||
|
||
interrupt := make(chan os.Signal, 1) | ||
signal.Notify(interrupt, os.Interrupt) | ||
<-interrupt | ||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) | ||
defer cancel() | ||
proxyServer.Shutdown(ctx) | ||
} |
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
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