|
| 1 | +{# |
| 2 | +# Copyright 2019 Google LLC |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +#} |
| 16 | +<!doctype html> |
| 17 | +<html> |
| 18 | + <head> |
| 19 | + <title>Google App Engine Flexible Environment - PHP Websockets Chat</title> |
| 20 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 21 | + </head> |
| 22 | + <body> |
| 23 | + |
| 24 | + <!-- [START gae_flex_websockets_form] --> |
| 25 | + <p>Chat demo</p> |
| 26 | + <form id="chat-form"> |
| 27 | + <textarea id="chat-text" placeholder="Enter some text..."></textarea> |
| 28 | + <button type="submit">Send</button> |
| 29 | + </form> |
| 30 | + |
| 31 | + <div> |
| 32 | + <p>Messages:</p> |
| 33 | + <ul id="chat-response"></ul> |
| 34 | + </div> |
| 35 | + |
| 36 | + <div> |
| 37 | + <p>Status:</p> |
| 38 | + <ul id="chat-status"></ul> |
| 39 | + </div> |
| 40 | + <!-- [END gae_flex_websockets_form] --> |
| 41 | + |
| 42 | + <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script> |
| 43 | + <script> |
| 44 | + // [START gae_flex_websockets_js] |
| 45 | + $(function() { |
| 46 | + /* If the main page is served via https, the WebSocket must be served via |
| 47 | + "wss" (WebSocket Secure) */ |
| 48 | + var scheme = window.location.protocol == "https:" ? 'wss://' : 'ws://'; |
| 49 | + var webSocketUri = scheme |
| 50 | + + window.location.hostname |
| 51 | + + (location.port ? ':'+location.port: '') |
| 52 | + + '/chat'; |
| 53 | + /* Get elements from the page */ |
| 54 | + var form = $('#chat-form'); |
| 55 | + var textarea = $('#chat-text'); |
| 56 | + var output = $('#chat-response'); |
| 57 | + var status = $('#chat-status'); |
| 58 | + /* Helper to keep an activity log on the page. */ |
| 59 | + function log(text){ |
| 60 | + status.append($('<li>').text(text)) |
| 61 | + } |
| 62 | + /* Establish the WebSocket connection and register event handlers. */ |
| 63 | + var websocket = new WebSocket(webSocketUri); |
| 64 | + websocket.onopen = function() { |
| 65 | + log('Connected'); |
| 66 | + }; |
| 67 | + websocket.onclose = function() { |
| 68 | + log('Closed'); |
| 69 | + }; |
| 70 | + websocket.onmessage = function(e) { |
| 71 | + log('Message received'); |
| 72 | + output.append($('<li>').text(e.data)); |
| 73 | + }; |
| 74 | + websocket.onerror = function(e) { |
| 75 | + log('Error (see console)'); |
| 76 | + console.log(e); |
| 77 | + }; |
| 78 | + /* Handle form submission and send a message to the websocket. */ |
| 79 | + form.submit(function(e) { |
| 80 | + e.preventDefault(); |
| 81 | + var data = textarea.val(); |
| 82 | + websocket.send(data); |
| 83 | + }); |
| 84 | + }); |
| 85 | + // [END gae_flex_websockets_js] |
| 86 | + </script> |
| 87 | + </body> |
| 88 | +</html> |
0 commit comments