我想使用WP-API心跳来打开到两个显示器的连接,并让它们反映另一个显示器对每个显示器所说的话send
和tick
.
当心跳API在3.6中出现时,它曾经可以工作,但现在在最新版本中,它输出了一个错误:
SyntaxError: JSON Parse error: Unexpected identifier "SyntaxError"
我曾尝试在web上遵循其他答案来解析json错误,或找到它失败的地方,但除了返回与服务器响应相同的url之外,似乎什么都没有出来。
function mb_heartbeat() {
wp_enqueue_script( \'heartbeat\' );
add_action( \'wp_footer\', \'mb_heartbeat_footer\' );
}
//our js to send/process
function mb_heartbeat_footer() { ?>
<script>
$(document).ready( function() {
// send the user id
$(document).on( "heartbeat-send.mb-location-change", function( event, data ) {
data.mb_user_id = $(\'input[name="mb_user_id"]\').val();
});
// receive the location
$(document).on( "heartbeat-tick.mb-location-change", function( event, data ) {
data.mb_user_location && $("input#mb_user_location" + data.location ).prop( "checked","checked" );
});
// log errors
$(document).on( "heartbeat-error.mb-location-change", function( event, jqXHR, textStatus, error ) {
console.log( textStatus + \' ----------- \' + error );
})
});
</script>
<?php }
// server-side code
function mb_heartbeat_received( $response, $data, $screen_id ) {
$mb_userid = ( empty($data[\'mb_user_id\']) || !is_numeric($data[\'mb_user_id\']) ? null : $data[\'mb_user_id\'] );
$mb_userid = absint( $mb_userid );
if( !$mb_userid ) {
return $response;
}
$response[\'mb_user_location\'] = get_user_meta( $mb_userid, \'mb_user_location_current\', true );
return $response;
}
// do it
add_action( \'init\', \'mb_heartbeat\' );
add_filter( \'heartbeat_received\', \'mb_heartbeat_received\', 10, 2 );
add_filter( \'heartbeat_settings\', \'mb_heartbeat_settings\' );