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 >
0 commit comments