WAMP.2怎么样?

时间:2018-03-09 14:16:41

标签: python websocket twisted autobahn wamp-protocol

似乎很难找到关于WAMP.2协议的任何内容。我正在尝试使用Python,高速公路和扭曲连接到使用WAMP.2的webSocket。但是,我一直收到这个错误:

    }

    private class DirectionsJSONParser {
        /**
         * Receives a JSONObject and returns a list of lists containing latitude and longitude
         */
        public List<List<HashMap<String, String>>> parse(JSONObject jObject) {
            List<List<HashMap<String, String>>> routes = new ArrayList<>();
            JSONArray jRoutes;
            JSONArray jLegs;
            JSONArray jSteps;
            try {
                jRoutes = jObject.getJSONArray("routes");
                for (int i = 0; i < jRoutes.length(); i++) {
                    jLegs = ((JSONObject) jRoutes.get(i)).getJSONArray("legs");
                    List path = new ArrayList<>();

                    for (int j = 0; j < jLegs.length(); j++) {
                        jSteps = ((JSONObject) jLegs.get(j)).getJSONArray("steps");
                        for (int k = 0; k < jSteps.length(); k++) {
                            String polyline = "";
                            polyline = (String) ((JSONObject) ((JSONObject) jSteps.get(k)).get("polyline")).get("points");
                            List<LatLng> list = decodePoly(polyline);
                            for (int l = 0; l < list.size(); l++) {
                                HashMap<String, String> hm = new HashMap<>();
                                hm.put("lat", Double.toString((list.get(l)).latitude));
                                hm.put("lng", Double.toString((list.get(l)).longitude));
                                path.add(hm);
                            }
                        }
                        routes.add(path);
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            } catch (Exception e) {

            }
            return routes;
        }

        /**
         *      * Method to decode polyline points
         *      * Courtesy : https://jeffreysambells.com/2010/05/27/decoding-polylines-from-google-maps-direction-api-with-java
         *      *
         */
        private List<LatLng> decodePoly(String encoded) {
            List<LatLng> poly = new ArrayList<>();
            int index = 0, len = encoded.length();
            int lat = 0, lng = 0;
            while (index < len) {
                int b, shift = 0, result = 0;
                do {
                    b = encoded.charAt(index++) - 63;
                    result |= (b & 0x1f) << shift;
                    shift += 5;
                } while (b >= 0x20);
                int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
                lat += dlat;
                shift = 0;
                result = 0;
                do {
                    b = encoded.charAt(index++) - 63;
                    result |= (b & 0x1f) << shift;
                    shift += 5;
                } while (b >= 0x20);
                int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
                lng += dlng;
                LatLng p = new LatLng((((double) lat / 1E5)),
                        (((double) lng / 1E5)));
                poly.add(p);
            }
            return poly;
        }

    }
}

private String getDirectionsUrl(LatLng origin, LatLng dest) {

    // Origin of route
    String str_origin = "origin=" + origin.latitude + "," + origin.longitude;

    // Destination of route
    String str_dest = "destination=" + dest.latitude + "," + dest.longitude;

    // Sensor enabled
    String sensor = "sensor=false";
    String mode = "mode=driving";
    String key="AIzaSyDz6HymXsuQbqGJkN70OVzNzNTr3pN9xBY";

    // Building the parameters to the web service
    String parameters = str_origin + "&" + str_dest + "&" + sensor + "&" + mode+ "&" + key;

    // Output format
    String output = "json";

    // Building the url to the web service
    String url = "https://maps.googleapis.com/maps/api/directions/" + output + "?" + parameters;


    return url;
}

private String downloadUrl(String strUrl) throws IOException {
    String data = "";
    InputStream iStream = null;
    HttpURLConnection urlConnection = null;
    try {
        URL url = new URL(strUrl);
        urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.connect();
        iStream = urlConnection.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(iStream));
        StringBuffer sb = new StringBuffer();
        String line = "";
        while ((line = br.readLine()) != null) {
            sb.append(line);
        }
        data = sb.toString();
        Log.d("downloadUrl", data.toString());
        br.close();
    } catch (Exception e) {
        Log.d("Exception", e.toString());
    } finally {
        iStream.close();
        urlConnection.disconnect();
    }
    return data;
}

//***************DIRECTION API CLOSE *******************************************************************************************

现在,通常情况下,当我遇到困难时,我至少可以在互联网上找到它。但是,似乎没有关于此的信息(谷歌甚至没有给出任何与WAMP.2相关的结果)。

我想如果webSocket服务器正在使用WAMP.2,必须有一种方法可以连接它们,对吧?如果是这样,为什么很难找到任何关于它的东西呢?

我正在使用的代码:

2018-03-09 14:54:53+0100 [-] Log opened.
2018-03-09 14:54:53+0100 [-] Starting factory 
<autobahn.twisted.websocket.WebSocketClientFactory object at 0x000002A461F489B0>
2018-03-09 14:54:53+0100 [-] failing WebSocket opening handshake ('WebSocket connection upgrade failed (400 -ThisserveronlyspeaksWebSocketsubprotocolswamp.2.cbor.batched,wamp.2.cbor,wamp.2.msgpack.batched,wamp.2.msgpack,wamp.2.ubjson.batched,wamp.2.ubjson,wamp.2.json.batched,wamp.2.json)')
2018-03-09 14:54:53+0100 [-] dropping connection to peer tcp4:... with abort=True: WebSocket connection upgrade failed (400 - ThisserveronlyspeaksWebSocketsubprotocolswamp.2.cbor.batched,wamp.2.cbor,wamp.2.msgpack.batched,wamp.2.msgpack,wamp.2.ubjson.batched,wamp.2.ubjson,wamp.2.json.batched,wamp.2.json)
2018-03-09 14:54:53+0100 [-] Stopping factory 
<autobahn.twisted.websocket.WebSocketClientFactory object at 0x000002A461F489B0>
2018-03-09 14:55:01+0100 [-] Received SIGINT, shutting down.
2018-03-09 14:55:01+0100 [-] Main loop terminated.

1 个答案:

答案 0 :(得分:0)

WAMP是一个位于WebSocket之上的协议,你需要一个实现它的库。

Autobahn | Python会这样做,但您正在尝试与WAMP路由器建立纯WebSocket连接。这自然会失败。

有关如何从Python连接到WAMP路由器的基本示例,请参阅例如https://github.com/crossbario/crossbar-examples/tree/master/hello/python

附注:WAMP v2是http://wamp-proto.org/implementations/中列出的所有实现正在运行的内容。版本1几乎已经被历史书籍所取代。