Skip to content

Commit a3a0d3e

Browse files
author
ace-n
committed
Address comments
1 parent 679b010 commit a3a0d3e

File tree

6 files changed

+104
-94
lines changed

6 files changed

+104
-94
lines changed

appengine/flexible/websockets/composer.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
"require": {
33
"cboden/ratchet": "^0.4.1",
44
"guzzlehttp/psr7": "^1.5",
5-
"silex/silex": "^2.3",
6-
"symfony/twig-bridge": "^4.2",
7-
"twig/twig": "^1.29"
5+
"silex/silex": "^2.3"
86
},
97
"require-dev": {
108
"phpunit/phpunit": "^7.5",
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
14+
>
15+
<html>
16+
<head>
17+
<title>Google App Engine Flexible Environment - PHP Websockets Chattitle>
18+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
19+
<meta charset="utf-8">
20+
<style>
21+
* { margin: 0; padding: 0; box-sizing: border-box; }
22+
body { font: 13px Helvetica, Arial; }
23+
form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }
24+
form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; }
25+
form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; }
26+
#messages { list-style-type: none; margin: 0; padding: 0; }
27+
#messages li { padding: 5px 10px; }
28+
#messages li:nth-child(odd) { background: #dedede; }
29+
#messages li:last-child { background: #aea; }
30+
section {
31+
background-color: #eee;
32+
border: 3px dashed #888; border-radius: 10px;
33+
margin: 30px; margin-bottom: 80px;
34+
padding: 5px;
35+
}
36+
style>
37+
head>
38+
<body>
39+
40+
41+
<h1>Websockets Chat Demoh1>
42+
43+
<form id="chat-form">
44+
<input type="text" id="chat-text" autocomplete="off" placeholder="Enter some text...">
45+
<button type="submit">Sendbutton>
46+
form>
47+
48+
<section>
49+
50+
<ul id="messages">ul>
51+
section>
52+
53+
54+
55+
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js">script>
56+
<script>
57+
// [START gae_flex_websockets_js]
58+
$(function() {
59+
/* If the main page is served via https, the WebSocket must be served via
60+
"wss" (WebSocket Secure) */
61+
var scheme = window.location.protocol == "https:" ? 'wss://' : 'ws://';
62+
var webSocketUri = scheme
63+
+ window.location.hostname
64+
+ (location.port ? ':'+location.port: '')
65+
+ '/ws';
66+
/* Helper to keep an activity log on the page. */
67+
function log(text, label) {
68+
label = label || 'Status';
69+
$('#messages').append(`
  • ${label}: ${text}`);
  • 70+
    }
    71+
    /* Establish the WebSocket connection and register event handlers. */
    72+
    var websocket = new WebSocket(webSocketUri);
    73+
    websocket.onopen = function() {
    74+
    log('Connected');
    75+
    };
    76+
    websocket.onclose = function() {
    77+
    log('Closed');
    78+
    };
    79+
    websocket.onmessage = function(e) {
    80+
    log(e.data, 'Message received')
    81+
    };
    82+
    websocket.onerror = function(e) {
    83+
    log('Error (see console)');
    84+
    console.log(e);
    85+
    };
    86+
    /* Handle form submission and send a message to the websocket. */
    87+
    $('#chat-form').submit(function(e) {
    88+
    e.preventDefault();
    89+
    var data = $('#chat-text').val();
    90+
    if (data) {
    91+
    websocket.send(data);
    92+
    window.scrollTo(0, document.body.scrollHeight)
    93+
    $('#chat-text').val('');
    94+
    }
    95+
    });
    96+
    });
    97+
    // [END gae_flex_websockets_js]
    98+
    script>
    99+
    body>
    100+
    html>

    appengine/flexible/websockets/index.html.twig

    Lines changed: 0 additions & 88 deletions
    This file was deleted.

    appengine/flexible/websockets/index.php

    Lines changed: 1 addition & 1 deletion
    Original file line numberDiff line numberDiff line change
    @@ -33,7 +33,7 @@
    3333
    $app->register(new Silex\Provider\RoutingServiceProvider());
    3434

    3535
    $app->get('/', function () use ($app) {
    36-
    return $app['twig']->render('index.html.twig');
    36+
    return file_get_contents('index.html');
    3737
    });
    3838

    3939
    // @codeCoverageIgnoreStart

    appengine/flexible/websockets/nginx-app.conf

    Lines changed: 1 addition & 1 deletion
    Original file line numberDiff line numberDiff line change
    @@ -1,4 +1,4 @@
    1-
    location /chat {
    1+
    location /ws {
    22
    proxy_pass "http://localhost:8000";
    33
    proxy_http_version 1.1;
    44
    proxy_set_header Upgrade $http_upgrade;

    appengine/flexible/websockets/test/system_test.php

    Lines changed: 1 addition & 1 deletion
    Original file line numberDiff line numberDiff line change
    @@ -38,7 +38,7 @@ protected function setUp()
    3838
    public function testIndex()
    3939
    {
    4040
    $connector = new Client\Connector($this->loop);
    41-
    $basePromise = $connector('ws://nodejs-docs-samples.appspot.com/chat')
    41+
    $basePromise = $connector('ws://' . getenv('GCLOUD_PROJECT') . '.appspot.com/ws')
    4242
    ->then(function ($conn) {
    4343
    $endPromise = new Promise(function ($resolve) use ($conn) {
    4444
    $conn->on('message', function ($msg) use ($resolve, $conn) {

    0 commit comments

    Comments
     (0)