有没有人能够在 ChromeCast API 中添加自定义命令?我成功地将井字游戏示例与我的开发人员 ID 以及修改后的协议字符串(在客户端和服务器上都更改了)一起工作。
在 Android 端,我有现有的“加入”命令,我正在添加一个新的“图像”命令:
public final void join(String name) {
try {
Log.d(TAG, "join: " + name);
JSONObject payload = new JSONObject();
payload.put(KEY_COMMAND, KEY_JOIN);
payload.put(KEY_NAME, name);
sendMessage(payload);
} catch (JSONException e) {
Log.e(TAG, "Cannot create object to join a game", e);
} catch (IOException e) {
Log.e(TAG, "Unable to send a join message", e);
} catch (IllegalStateException e) {
Log.e(TAG, "Message Stream is not attached", e);
}
}
public final void sendImage(String sURL) {
try {
Log.d(TAG, "sendImage");
JSONObject payload = new JSONObject();
payload.put(KEY_COMMAND, KEY_IMAGE);
payload.put(KEY_URL, sURL);
sendMessage(payload);
} catch (JSONException e) {
Log.e(TAG, "Cannot create object to send image", e);
} catch (IOException e) {
Log.e(TAG, "Unable to send an image message", e);
} catch (IllegalStateException e) {
Log.e(TAG, "Message Stream is not attached", e);
}
}
如果我调用 join 命令,它可以正常工作,并且我可以在浏览器中看到通过控制台记录的消息。但是如果我调用 sendImage 函数,我会收到以下错误:
“onEnded 连接通道失败:协议错误”
在 ChromeCast 方面,我可以看到收到有效命令的时间。当我发送加入命令时会调用此函数,但在发送自定义“图像”命令时不会调用此函数。
/**
* Message received event; determines event message and command, and
* choose function to call based on them.
* @param {event} event the event to be processed.
*/
onMessage: function(event) {
console.log('***== pre onMessage ==***');
var message = event.message;
var channel = event.target;
console.log('********onMessage********' + JSON.stringify(message));
console.log('mPlayer1: ' + this.mPlayer1);
console.log('mPlayer2: ' + this.mPlayer2);
if (message.command == 'join') {
this.onJoin(channel, message);
} else if (message.command == 'leave') {
this.onLeave(channel);
} else if (message.command == 'move') {
this.onMove(channel, message);
} else if (message.command == 'queue_layout_request') {
this.onQueueLayoutRequest(channel);
} else if (message.command == 'image') {
this.onImage(channel, message);
} else if (message.command == 'video') {
this.onVideo(channel, message);
} else if (message.command == 'song') {
this.onSong(channel, message);
} else {
cast.log.error('Invalid message command: ' + message.command);
}
},
有任何想法吗?在其他地方我需要定义我的自定义命令吗?
已编辑:还显示了 onImage 原型:
/**
* Image event: display an image
* @param {cast.receiver.channel} channel the source of the move, which
* determines the player.
* @param {Object|string} message contains the URL of the image
*/
onImage: function(channel, message) {
console.log('****onImage: ' + JSON.stringify(message));
//Hide video and show image
mVideo.style.visibility='hidden';
mImage.style.visibility='visible';
mImage.src = message.url;
},