gpt4 book ai didi

java - onmessage Google App Engine (Java) channel API

转载 作者:行者123 更新时间:2023-12-01 05:18:11 25 4
gpt4 key购买 nike

我正在尝试使用 Google App Engine 的 Channel API。

JavaScript/JQuery:

$(document).ready(function(){
alert('ready');

$.post('/token', function(data) {
alert('token:' + data['token']);
openChannel( data['token'] );
});

$.post('/chat', function(data) {
alert('chat:' + data['users'].length);
});

});


onMessage = function(message) {
alert(message);
}

onSocketError = function(error){
alert("Error is <br/>"+error.description+" <br /> and HTML code"+error.code);
};

onSocketOpen = function() {
// socket opened
};

onSocketClose = function() {
alert("Socket Connection closed");
};

openChannel = function(token) {
alert('open channel');
var channel = new goog.appengine.Channel( token );
var socket = channel.open();
socket.onopen = onSocketOpen;
socket.onmessage = onMessage;
socket.onerror = onSocketError;
socket.onclose = onSocketClose;
};

问题是警报(消息)没有触发。我的代码中有什么幸运的地方?

我对服务器端具有 "\\{\\{ token\\}\\}"channel = new goog.appengine.Channel(' 的一些示例感到困惑JavaScript 中的 {{ token }}')

{{ }} 中包含的内容是什么?

最佳答案

请注意 token 是标识您的页面的 token key 。首先在页面中初始化 token ,例如:

 ChannelService channelService = ChannelServiceFactory.getChannelService();
String token = channelService.createChannel("sample");

现在

var token ="<%=token %>";// This will creaete unique identifier(some id created by google api + ur key)

channel = new goog.appengine.Channel('<%=token%>');
socket = channel.open();

socket.onopen = function () {
var connected = true;
sendMessage('<b>'+userName+'</b> Logged in.');
};

..创建这样的函数

关于java - onmessage Google App Engine (Java) channel API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10878364/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com