Skip to content
This repository has been archived by the owner on May 27, 2024. It is now read-only.

Commit

Permalink
sync with server ping update
Browse files Browse the repository at this point in the history
  • Loading branch information
codeskyblue committed Oct 31, 2017
1 parent b65341b commit 2b5272d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 25 deletions.
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,8 @@ func main() {
}
}

fmt.Printf("atx-agent version %s\n", version)

// show ip
outIp, err := getOutboundIP()
if err == nil {
Expand Down
37 changes: 12 additions & 25 deletions tunnelproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ func runTunnelProxy(serverAddr string) {
}

func unsafeRunTunnelProxy(serverAddr string) error {
c, _, err := websocket.DefaultDialer.Dial("ws://"+serverAddr+"/echo", nil)
ws, _, err := websocket.DefaultDialer.Dial("ws://"+serverAddr+"/echo", nil)
if err != nil {
return err
}
defer c.Close()
defer ws.Close()
log.Printf("server connected")

props, _ := androidutils.Properties()
devInfo := &proto.DeviceInfo{
Expand All @@ -42,35 +43,21 @@ func unsafeRunTunnelProxy(serverAddr string) error {
AgentVersion: version,
}
devInfo.HWAddr, _ = androidutils.HWAddrWLAN()
c.WriteJSON(proto.CommonMessage{
ws.WriteJSON(proto.CommonMessage{
Type: proto.DeviceInfoMessage,
Data: devInfo,
})

readQuitChan := make(chan error, 1)
go func() {
for {
_, data, err := c.ReadMessage()
if err != nil {
readQuitChan <- err
break
}
_ = data
// log.Printf("Websocket receive message: %v", string(data))
}
}()
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
ws.SetPingHandler(func(string) error {
ws.WriteMessage(websocket.PongMessage, []byte{})
return nil
})
for {
select {
case t := <-ticker.C:
err := c.WriteMessage(websocket.TextMessage, []byte(t.String()))
if err != nil {
log.Println(err)
return err
}
case err := <-readQuitChan:
_, data, err := ws.ReadMessage()
if err != nil {
return err
}
_ = data
// log.Printf("Websocket receive message: %v", string(data))
}
}

0 comments on commit 2b5272d

Please sign in to comment.