javascript 我如何在 html5 的谷歌应用引擎上创建一个 websocket

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4121920/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 10:26:02  来源:igfitidea点击:

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

javascriptpythongoogle-app-enginehtmlwebsocket

提问by zjm1126

this is the demothat a Simple chat client , you must open it on webkit Browser like: chrome and Safari,

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

the demo use a web socket server based on node.js: websocket-server-node.js,

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

but i think it cant Deploy on google app engine ,

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

so did you know how to make a websocket using python on google app engine ,

那么你知道如何在谷歌应用引擎上使用 python 制作 websocket 吗?

and running the demo on it ,

并在其上运行演示,

thanks

谢谢

this is the code :

这是代码:

<!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>

回答by systempuntoout

I think you should wait for Channel API.

我认为你应该等待Channel API

Channel API- The Channel API lets you build applications that can push content directly to your user's browser (aka “Comet”). No more polling for updates!

Channel API- Channel API 允许您构建可以将内容直接推送到用户浏览器(又名“Comet”)的应用程序。没有更多的轮询更新!

this is already part of the SDK but does not work in production.

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

Herea video that show this new upcoming feature
Herea demo application with a multiplayer trivia-quiz

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

EDIT:
available with SDK 1.4.0

编辑:
可用于SDK 1.4.0

回答by Moishe Lettvin

Note that this has now been released: http://code.google.com/appengine/docs/python/channel/

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

Enjoy!

享受!

回答by alvion

Yes you can, as was pointed out in a comment on a related question: How can I run a WebSocket server on top of GAE?

是的,正如在对相关问题的评论中指出的那样: 如何在 GAE 之上运行 WebSocket 服务器?

The details of implementing this using code from the Typhoon app server are detailed here: https://code.google.com/p/typhoonae/wiki/WebSockets

使用来自 Typhoon 应用服务器的代码实现此功能的详细信息在此处详细说明:https: //code.google.com/p/typhoonae/wiki/WebSockets

You may have difficulties with the 30 second timeout issue, but as has been pointed out elsewhere, you can cause your client to reconnect to the server every 25 seconds or so to preserve a constant connection. Edge cases where partial messages are sent to more than one socket may be tricky but should be solvable.

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

Using Channels is probably preferable if you are using Javascript for the client, as the OP seems to be. Websockets may be easier though if you have existing code that already uses WS or if you need to support non-JS clients that do not yet have Channels support.

如果您为客户端使用 Javascript,则使用 Channels 可能更可取,因为 OP 似乎是这样。如果您有已经使用 WS 的现有代码,或者如果您需要支持尚不支持 Channels 的非 JS 客户端,Websockets 可能会更容易。

回答by Ivo Wetzel

You could use one of the Python based WebSocket Server implementations that's out there, but you won't make it far with that on AppEngine since it's request based and every request has a maximum lifetime of 30 seconds, you'll need a VPS or something the like, if you want to host a chat server, and in that case, you could just as well run the Node.js version.

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

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

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