我开发WebRtc移动应用程序。我从加载源代码
https://webrtc.googlesource.com/src
并准备一个移动应用
https://webrtc.org/native-code/development/
2.我从https://github.com/webrtc/apprtc加载webrtc信号服务器
我使用对撞机信号服务器https://github.com/webrtc/apprtc/tree/master/src/collider
我尝试将移动应用程序与信号服务器http://my_server.net
连接建立连接后,我从应用程序发送了一个请求,信号服务器向我发送了答复。服务器向我发送“确定”
io.WriteString(w, "OK\n")
但移动应用程序正在等待包含JSON格式参数的响应。在文件org / appspot / apprtc / RoomParametersFetcher.java中,函数
void roomHttpResponseParse(String response)
以JSON格式解析响应
我找不到JSON格式的信号服务器的响应。
WebRtc信号服务器在Windows 7上运行。客户端应用程序在Android上运行。
信号服务器端
func (c *Collider) httpHandler(w http.ResponseWriter, r *http.Request)
{
w.Header().Add("Access-Control-Allow-Origin", "*")
w.Header().Add("Access-Control-Allow-Methods", "POST, DELETE")
p := strings.Split(r.URL.Path, "/")
if len(p) != 3 {
c.httpError("Invalid path: "+html.EscapeString(r.URL.Path), w)
return
}
rid, cid := p[1], p[2]
switch r.Method {
case "POST":
body, err := ioutil.ReadAll(r.Body)
if err != nil {
c.httpError("Failed to read request body: "+err.Error(), w)
return
}
m := string(body)
if m == "" {
c.httpError("Empty request body", w)
return
}
if err := c.roomTable.send(rid, cid, m); err != nil {
c.httpError("Failed to send the message: "+err.Error(), w)
return
}
case "DELETE":
c.roomTable.remove(rid, cid)
default:
log.Printf("console httpHandler: r.URL.Path=%s", r.URL.Path)
c.httpError("browser default:", w)
return
}
io.WriteString(w, "OK\n")
}
客户端
private void roomHttpResponseParse(String response)
{
Log.d(TAG, "Room response: " + response);
try {
LinkedList<IceCandidate> iceCandidates = null;
SessionDescription offerSdp = null;
JSONObject roomJson = new JSONObject(response);
String result = roomJson.getString("result");
if (!result.equals("SUCCESS")) {
events.onSignalingParametersError("Room response error: " + result);
return;
}
response = roomJson.getString("params");
roomJson = new JSONObject(response);
String roomId = roomJson.getString("room_id");
String clientId = roomJson.getString("client_id");
String wssUrl = roomJson.getString("wss_url");
String wssPostUrl = roomJson.getString("wss_post_url");
boolean initiator = (roomJson.getBoolean("is_initiator"));
String roomUrl =
registerUrl.substring(0, registerUrl.indexOf("/register"));
if (!initiator) {
iceCandidates = new LinkedList<IceCandidate>();
String messagesString = roomJson.getString("messages");
JSONArray messages = new JSONArray(messagesString);
for (int i = 0; i < messages.length(); ++i) {
String messageString = messages.getString(i);
JSONObject message = new JSONObject(messageString);
String messageType = message.getString("type");
Log.d(TAG, "GAE->C #" + i + " : " + messageString);
if (messageType.equals("offer")) {
offerSdp = new SessionDescription(
SessionDescription.Type.fromCanonicalForm(messageType),
message.getString("sdp"));
} else if (messageType.equals("candidate")) {
IceCandidate candidate = new IceCandidate(
message.getString("id"),
message.getInt("label"),
message.getString("candidate"));
iceCandidates.add(candidate);
} else {
Log.e(TAG, "Unknown message: " + messageString);
}
}
}
Log.d(TAG, "RoomId: " + roomId + ". ClientId: " + clientId);
Log.d(TAG, "Initiator: " + initiator);
Log.d(TAG, "Room url: " + roomUrl);
Log.d(TAG, "WSS url: " + wssUrl);
Log.d(TAG, "WSS POST url: " + wssPostUrl);
LinkedList<PeerConnection.IceServer> iceServers =
iceServersFromPCConfigJSON(roomJson.getString("pc_config"));
boolean isTurnPresent = false;
for (PeerConnection.IceServer server : iceServers) {
Log.d(TAG, "IceServer: " + server);
if (server.uri.startsWith("turn:")) {
isTurnPresent = true;
break;
}
}
if (!isTurnPresent) {
LinkedList<PeerConnection.IceServer> turnServers =
requestTurnServers(roomJson.getString("turn_url"));
for (PeerConnection.IceServer turnServer : turnServers) {
Log.d(TAG, "TurnServer: " + turnServer);
iceServers.add(turnServer);
}
}
MediaConstraints pcConstraints = constraintsFromJSON(
roomJson.getString("pc_constraints"));
addDTLSConstraintIfMissing(pcConstraints, loopback);
Log.d(TAG, "pcConstraints: " + pcConstraints);
MediaConstraints videoConstraints = constraintsFromJSON(
getAVConstraints("video",
roomJson.getString("media_constraints")));
Log.d(TAG, "videoConstraints: " + videoConstraints);
MediaConstraints audioConstraints = constraintsFromJSON(
getAVConstraints("audio",
roomJson.getString("media_constraints")));
Log.d(TAG, "audioConstraints: " + audioConstraints);
SignalingParameters params = new SignalingParameters(
iceServers, initiator,
pcConstraints, videoConstraints, audioConstraints,
roomUrl, roomId, clientId,
wssUrl, wssPostUrl,
offerSdp, iceCandidates);
events.onSignalingParametersReady(params);
} catch (JSONException e) {
events.onSignalingParametersError(
"Room JSON parsing error: " + e.toString());
} catch (IOException e) {
events.onSignalingParametersError("Room IO error: " + e.toString());
}
}
我希望信号服务器将以JSON格式发送正确的答案。
非常感谢您的帮助。