Skip to content
This repository has been archived by the owner on Jan 13, 2023. It is now read-only.

Commit

Permalink
fix always connect ws bug
Browse files Browse the repository at this point in the history
  • Loading branch information
codeskyblue committed Feb 28, 2017
1 parent d98a83f commit a7c331d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 41 deletions.
47 changes: 6 additions & 41 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ package main

import (
"bufio"
"encoding/json"
"path/filepath"
// "flag"
"fmt"

"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
Expand All @@ -22,8 +20,8 @@ import (
"github.com/gorilla/mux"
accesslog "github.com/mash/go-accesslog"
flag "github.com/ogier/pflag"
"github.com/openatx/wdaproxy/connector"
"github.com/openatx/wdaproxy/web"
"github.com/qiniu/log"
)

var (
Expand Down Expand Up @@ -77,41 +75,6 @@ type Device struct {
Manufacturer string `json:"manufacturer"`
}

func mockIOSProvider() {
c := connector.New(yosemiteServer, yosemiteGroup, lisPort)
go c.KeepOnline()

device, err := GetDeviceInfo(getUdid())
if err != nil {
log.Fatal(err)
}

c.WriteJSON(map[string]interface{}{
"type": "addDevice",
"data": device,
})

rt.HandleFunc("/api/devices/{udid}/remoteConnectUrl", func(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
json.NewEncoder(w).Encode(map[string]interface{}{
"success": true,
"description": "notice this is mock data",
"remoteConnectUrl": fmt.Sprintf("http://%s:%d/", c.RemoteIp, lisPort),
})
}
if r.Method == "DELETE" {
json.NewEncoder(w).Encode(map[string]interface{}{
"success": true,
"description": "Device remote disconnected successfully",
})
}
})

rt.HandleFunc("/devices/{udid}", func(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "Not finished yet")
})
}

func main() {
showVer := flag.BoolP("version", "v", false, "Print version")
flag.IntVarP(&lisPort, "port", "p", 8100, "Proxy listen port")
Expand All @@ -129,13 +92,15 @@ func main() {
udid = getUdid()
}

mockIOSProvider()

if *showVer {
println(version)
return
}

if yosemiteServer != "" {
mockIOSProvider()
}

errC := make(chan error)
freePort, err := freeport.Get()
if err != nil {
Expand Down
46 changes: 46 additions & 0 deletions provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package main

import (
"encoding/json"
"fmt"
"io"
"net/http"

"github.com/openatx/wdaproxy/connector"
"github.com/qiniu/log"
)

func mockIOSProvider() {
c := connector.New(yosemiteServer, yosemiteGroup, lisPort)
go c.KeepOnline()

device, err := GetDeviceInfo(udid)
if err != nil {
log.Fatal(err)
}

c.WriteJSON(map[string]interface{}{
"type": "addDevice",
"data": device,
})
rt.HandleFunc("/api/devices/{udid}/remoteConnect", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
if r.Method == "POST" {
json.NewEncoder(w).Encode(map[string]interface{}{
"success": true,
"description": "notice this is mock data",
"remoteConnectUrl": fmt.Sprintf("http://%s:%d/", c.RemoteIp, lisPort),
})
}
if r.Method == "DELETE" {
json.NewEncoder(w).Encode(map[string]interface{}{
"success": true,
"description": "Device remote disconnected successfully",
})
}
}).Methods("POST", "DELETE")

rt.HandleFunc("/devices/{udid}", func(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "Not finished yet")
})
}

0 comments on commit a7c331d

Please sign in to comment.