繁体   English   中英

我怎么能在谷歌应用程序引擎上为html5创建一个websocket

[英]how can i create a websocket on google app engine for html5

这是一个简单聊天客户端的演示 ,您必须在webkit浏览器上打开它,如:chrome和Safari,

该演示使用基于node.js的web套接字服务器:websocket-server-node.js,

但我认为它无法在谷歌应用引擎上部署,

所以你知道如何在谷歌应用引擎上使用python制作websocket,

并在其上运行演示,

谢谢

这是代码:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset=utf-8 />
<meta name="viewport" content="width=620" />
<title>HTML5 Demo: Web Socket</title>
<link rel="stylesheet" href="http://html5demos.com/css/html5demos.css" type="text/css" />
<script src="http://html5demos.com/js/h5utils.js"></script></head>
<body>
<section id="wrapper">
    <header>
      <h1>Web Socket</h1>
    </header>
<style>
#chat { width: 97%; }
.them { font-weight: bold; }
.them:before { content: 'them '; color: #bbb; font-size: 14px; }
.you { font-style: italic; }
.you:before { content: 'you '; color: #bbb; font-size: 14px; font-weight: bold; }
#log {
  overflow: auto;
  max-height: 300px;
  list-style: none;
  padding: 0;
/*  margin: 0;*/
}
#log li {
  border-top: 1px solid #ccc;
  margin: 0;
  padding: 10px 0;
}
</style>
<article>
  <form>
    <input type="text" id="chat" placeholder="type and press enter to chat" />
  </form>
  <p id="status">Not connected</p>
  <p>Users connected: <span id="connected">0</span></p>
  <p>To test, open two windows with Web Socket support, type a message above and press return.</p>
  <p>The server side code is available here: <a href="http://github.com/remy/html5demos/tree/master/server/">node-web-socket & server</a> (note that it runs on <a href="http://nodejs.org/" title="node.js">nodejs</a>)</p>
  <ul id="log"></ul>
</article>
<script>
function openConnection() {
  // uses global 'conn' object
  if (conn.readyState === undefined || conn.readyState > 1) {
    conn = new WebSocket('ws://node.remysharp.com:8001');    
    conn.onopen = function () {
      state.className = 'success';
      state.innerHTML = 'Socket open';
    };

    conn.onmessage = function (event) {
      var message = JSON.parse(event.data);
      if (typeof message == 'string') {
        log.innerHTML = '<li class="them">' + message.replace(/[<>&]/g, function (m) { return entities[m]; }) + '</li>' + log.innerHTML;
      } else {
        connected.innerHTML = message;
      }
    };

    conn.onclose = function (event) {
      state.className = 'fail';
      state.innerHTML = 'Socket closed';
    };
  }
}

var connected = document.getElementById('connected'),
    log = document.getElementById('log'),
    chat = document.getElementById('chat'),
    form = chat.form,
    conn = {},
    state = document.getElementById('status'),
    entities = {
      '<' : '<',
      '>' : '>',
      '&' : '&'
    };


if (window.WebSocket === undefined) {
  state.innerHTML = 'Sockets not supported';
  state.className = 'fail';
} else {
  state.onclick = function () {
    if (conn.readyState !== 1) {
      conn.close();
      setTimeout(function () {
        openConnection();
      }, 250);
    }
  };

  addEvent(form, 'submit', function (event) {
    event.preventDefault();

    // if we're connected
    if (conn.readyState === 1) {
      conn.send(JSON.stringify(chat.value));
      log.innerHTML = '<li class="you">' + chat.value.replace(/[<>&]/g, function (m) { return entities[m]; }) + '</li>' + log.innerHTML;

      chat.value = '';
    }
  });

  openConnection();  
}

</script>    <footer><a href="/">HTML5 demos</a>/<a id="built" href="http://twitter.com/rem">@rem built this</a>/<a href="#view-source">view source</a></footer> 
</section>
<a href="http://github.com/remy/html5demos"><img style="position: absolute; top: 0; left: 0; border: 0;" src="http://s3.amazonaws.com/github/ribbons/forkme_left_darkblue_121621.png" alt="Fork me on GitHub" /></a>

</body>
</html>

我认为你应该等待Channel API

通道API - 通道API允许您构建可以将内容直接推送到用户的浏览器(也称为“Comet”)的应用程序。 不再需要轮询更新!

这已经是SDK的一部分,但在生产中不起作用。

是一个显示即将推出的新功能的视频
是一个带有多人琐事测验的演示应用程序

编辑:
适用于SDK 1.4.0

请注意,现已发布: http//code.google.com/appengine/docs/python/channel/

请享用!

是的,你可以,正如在相关问题的评论中所指出的: 我如何在GAE之上运行WebSocket服务器?

使用Typhoon应用服务器代码实现此功能的详细信息请参见: https//code.google.com/p/typhoonae/wiki/WebSockets

您可能遇到30秒超时问题,但正如其他地方所指出的那样,您可以让客户端每25秒左右重新连接到服务器以保持连接不变。 将部分消息发送到多个套接字的边缘情况可能很棘手但应该是可解决的。

如果你正在使用Javascript作为客户端,那么使用频道可能更好。 如果现有代码已经使用了WS,或者如果您需要支持尚未支持Channels的非JS客户端,则Websockets可能会更容易。

你可以使用其中一个基于Python的WebSocket Server实现,但是你不会在AppEngine上做到这一点,因为它是基于请求的,并且每个请求的最大生命周期为30秒,你需要一个VPS或者其他东西之类的,如果你想托管一个聊天服务器,那么你也可以运行Node.js版本。

AppEngine限制: http//code.google.com/appengine/docs/python/runtime.html#Quotas_and_Limits

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM