首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >本地服务器的ApprtcDemo在浏览器之间工作,但不是浏览器本机的Android。

本地服务器的ApprtcDemo在浏览器之间工作,但不是浏览器本机的Android。
EN

Stack Overflow用户
提问于 2014-01-13 06:37:02
回答 1查看 9.1K关注 0票数 17

我正在开发一个聊天应用程序,并完成了它。现在我也想实现视频聊天。经过大量的研究,我决定去"WebRTC“图书馆。

我做了什么?

1)能够在本地服务器上运行AppRtcDemo,并能在浏览器之间正常工作。

参考资料:http://www.webrtc.org/reference/getting-started

2)能够在运行安卓AppRtcDemo.But时说“跨源不支持”。

经过研究,我发现在webrtc的讨论中,为了解决这个问题,我需要设置自己的转服务器。

3)所以我安装了webrtc推荐的最新rfc5766TurnServer。我成功地运行了转服务器。

参考资料:http://code.google.com/p/rfc5766-turn-server/

我做了ApprtcDemo (web)和(Android)的更改,以便与我的转服务器一起工作

1) apprtc.py

取代:

代码语言:javascript
复制
turn_url = 'https://computeengineondemand.appspot.com/'
turn_url = turn_url + 'turn?' + 'username=' + user + '&key=4080218913'

指向我的转服务器:

代码语言:javascript
复制
turn_url = 'http://192.168.5.85:3478/?service=turn&username=biraj'

2) index.html

取代:

代码语言:javascript
复制
var pcConfig = {{ pc_config|safe }};

通过以下方式:

代码语言:javascript
复制
var pcConfig = {"iceServers": [{"url": "stun:stun.l.google.com:19302"},            {"url":"turn:biraj@192.168.5.85:3479", "credential":"0x5b04123c3eec4cf0be64ab909bb2ff5b"}]};

Android

1)AppRTCDemoActivity.java

取代:

代码语言:javascript
复制
roomInput.setText("https://apprtc.appspot.com/?r=");

我的本地学徒服务器:

代码语言:javascript
复制
roomInput.setText("http://192.168.5.86:8080/?r=");

2) AppRTCClient.java

private PeerConnection.IceServer requestTurnServer(String url){}函数

取代:

代码语言:javascript
复制
connection.addRequestProperty("origin", "https://apprtc.appspot.com");

通过以下方式:

代码语言:javascript
复制
connection.addRequestProperty("origin", "http://192.168.5.86:8080");

3) /assets/channel.html el.html

取代:

代码语言:javascript
复制
<script src="https://apprtc.appspot.com/_ah/channel/jsapi"></script>

通过以下方式:

代码语言:javascript
复制
<script src="http://192.168.5.86:8080/_ah/channel/jsapi"></script>

现在我的问题是,为什么这是在浏览器之间工作,而不是在安卓AppRtcDemo和浏览器之间。

当我在android上运行AppRtcDemo后,在右上角启动本地摄像机预览,并提示“等待ICEcandidates”,那么什么也不会发生。

提前谢谢。

感谢所有支持我的question.After与ApprtcDemo长期崎岖的旅程,我取得了成功,它的工作很好。我张贴的解决方案。

查找"GAEChannelClient.java“java文件。

然后做如下的改变。

代码语言:javascript
复制
/*
 * libjingle
 * Copyright 2013, Google Inc.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  1. Redistributions of source code must retain the above copyright notice,
 *     this list of conditions and the following disclaimer.
 *  2. Redistributions in binary form must reproduce the above copyright notice,
 *     this list of conditions and the following disclaimer in the documentation
 *     and/or other materials provided with the distribution.
 *  3. The name of the author may not be used to endorse or promote products
 *     derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package org.appspot.apprtc;

import java.io.InputStream;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.util.Log;
import android.webkit.ConsoleMessage;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

/**
 * Java-land version of Google AppEngine's JavaScript Channel API:
 * https://developers.google.com/appengine/docs/python/channel/javascript
 * 
 * Requires a hosted HTML page that opens the desired channel and dispatches JS
 * on{Open,Message,Close,Error}() events to a global object named
 * "androidMessageHandler".
 */
public class GAEChannelClient {
    private static final String TAG = "GAEChannelClient";
    private WebView webView;
    private final ProxyingMessageHandler proxyingMessageHandler;

    /**
     * Callback interface for messages delivered on the Google AppEngine
     * channel.
     * 
     * Methods are guaranteed to be invoked on the UI thread of |activity|
     * passed to GAEChannelClient's constructor.
     */
    public interface MessageHandler {
        public void onOpen();

        public void onMessage(String data);

        public void onClose();

        public void onError(int code, String description);
    }

    /** Asynchronously open an AppEngine channel. */
    @SuppressLint("SetJavaScriptEnabled")
    public GAEChannelClient(Activity activity, String token, MessageHandler handler) {
        webView = new WebView(activity);

        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setAllowFileAccessFromFileURLs(true); // Maybe you
                                                                    // don't
                                                                    // need this
                                                                    // rule
        webView.getSettings().setAllowUniversalAccessFromFileURLs(true);

        webView.setWebChromeClient(new WebChromeClient() { // Purely for
                                                            // debugging.
            public boolean onConsoleMessage(ConsoleMessage msg) {
                Log.d(TAG, "console: " + msg.message() + " at " + msg.sourceId() + ":" + msg.lineNumber());
                return false;
            }
        });
        webView.setWebViewClient(new WebViewClient() { // Purely for debugging.
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                Log.e(TAG, "JS error: " + errorCode + " in " + failingUrl + ", desc: " + description);
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                System.out.println("HI");
                return super.shouldOverrideUrlLoading(view, url);
            }
        });

        proxyingMessageHandler = new ProxyingMessageHandler(activity, handler, token);
        webView.addJavascriptInterface(proxyingMessageHandler, "androidMessageHandler");
//       webView.loadUrl("file:///android_asset/channel.html");
        try {
            InputStream is = activity.getAssets().open("channel.html");
            StringBuilder builder = new StringBuilder();
            byte[] buffer = new byte[1024];
            while (is.read(buffer) != -1) {
                builder.append(new String(buffer));
            }
            is.close();
            String str = builder.toString();
            webView.loadDataWithBaseURL("http://192.168.5.86:8080", str, "text/html", "utf-8", null);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /** Close the connection to the AppEngine channel. */
    public void close() {
        if (webView == null) {
            return;
        }
        proxyingMessageHandler.disconnect();
        webView.removeJavascriptInterface("androidMessageHandler");
        webView.loadUrl("about:blank");
        webView = null;
    }

    // Helper class for proxying callbacks from the Java<->JS interaction
    // (private, background) thread to the Activity's UI thread.
    private static class ProxyingMessageHandler {
        private final Activity activity;
        private final MessageHandler handler;
        private final boolean[] disconnected = { false };
        private final String token;

        public ProxyingMessageHandler(Activity activity, MessageHandler handler, String token) {
            this.activity = activity;
            this.handler = handler;
            this.token = token;
        }

        public void disconnect() {
            disconnected[0] = true;
        }

        private boolean disconnected() {
            return disconnected[0];
        }

        @JavascriptInterface
        public String getToken() {
            return token;
        }

        @JavascriptInterface
        public void onOpen() {

            System.out.println("GAEClient : Open" );
            activity.runOnUiThread(new Runnable() {
                public void run() {
                    if (!disconnected()) {
                        handler.onOpen();
                    }
                }
            });
        }

        @JavascriptInterface
        public void onMessage(final String data) {
            System.out.println("GAEClient : Message : " +data );
            activity.runOnUiThread(new Runnable() {
                public void run() {
                    if (!disconnected()) {
                        handler.onMessage(data);
                    }
                }
            });
        }

        @JavascriptInterface
        public void onClose() {
            System.out.println("GAEClient : Close" );
            activity.runOnUiThread(new Runnable() {
                public void run() {
                    if (!disconnected()) {
                        handler.onClose();
                    }
                }
            });
        }

        @JavascriptInterface
        public void onError(final int code, final String description) {
            System.out.println("GAEClient : Erroe : " + description);
            activity.runOnUiThread(new Runnable() {
                public void run() {
                    if (!disconnected()) {
                        handler.onError(code, description);
                    }
                }
            });
        }
    }
}

资产文件夹中的Channel.html

代码语言:javascript
复制
<html>
  <head>
    <script src="http://192.168.5.86:8080/_ah/channel/jsapi"></script>
  </head>
  <!--
  Helper HTML that redirects Google AppEngine's Channel API to a JS object named
  |androidMessageHandler|, which is expected to be injected into the WebView
  rendering this page by an Android app's class such as AppRTCClient.
  -->
  <body onbeforeunload="closeSocket()" onload="openSocket()">
    <script type="text/javascript">
      var token = androidMessageHandler.getToken();
      if (!token)
        throw "Missing/malformed token parameter: [" + token + "]";

      var channel = null;
      var socket = null;

      function openSocket() {
        channel = new goog.appengine.Channel(token);
        socket = channel.open({
          'onopen': function() { androidMessageHandler.onOpen(); },
          'onmessage': function(msg) { androidMessageHandler.onMessage(msg.data); },
          'onclose': function() { androidMessageHandler.onClose(); },
          'onerror': function(err) { androidMessageHandler.onError(err.code, err.description); }
        });
      }

      function closeSocket() {
        socket.close();
      }
    </script>
  </body>
</html>
EN

回答 1

Stack Overflow用户

发布于 2014-01-18 22:30:20

遗憾的是,我不知道你是否做过这些事:

  1. 在每个应用程序(无论是PC还是移动)上使用相同的眩晕和打开服务器。
  2. 你是否甚至在申请之间发送ICE候选人(我认为你有,但只是为了核实)。
  3. 您确定眩晕/转url是导致错误的原因吗?因为我不能相信这些事情会影响跨源(它们不应该,因为您只是从客户端连接到服务器。跨原点主要用于从外部源加载数据的网页上。不允许在XHR中这样做)。我真的认为这与https://apprtc.appspot.com/_ah/channel/jsapi有关,因为这是跨源的一个很好的例子。

如果您打开在移动上的铬浏览器中工作的网页怎么办?那它会做什么呢?(请注意,您可以连接您的手机到您的个人电脑,以拥有完整的开发工具铬。Chrome在你的android设备上运行,但你可以在你的电脑上看到devtools )。

如果你能给我这些答案,我也许能帮你。尝试还原所有这些更改,只使用谷歌的转服务器,但只使该https://apprtc.appspot.com/_ah/channel/jsapi文件本地。

编辑:我看你找到了答案.你介意和我分享一下吗?

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21085261

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档