WebSocketを利用し、PHPにてチャットシステムを作成しようと勉強中です。
下記サイトを参考にて、動作することまでは確認しました。
http://challenge.no1s.biz/programming/php/158
具体的なコード
PHP
1server.php 2<?php 3use Ratchet\Server\IoServer; 4use MyApp\Chat; 5use Ratchet\Http\HttpServer; 6use Ratchet\WebSocket\WsServer; 7 8require dirname ( __DIR__ ) . '/vendor/autoload.php'; 9 10$server = IoServer::factory ( new HttpServer( new WsServer( new Chat () ) ), 8080 ); 11$server->run ();
コンソールで以下を実行
$ php server.php
HTML
1 2<!DOCTYPE html> 3<html lang="ja"> 4<meta charset="utf-8"> 5<title>Chat</title> 6<style> 7.container { 8 width: 600px; 9} 10.box{ 11 overflow:hidden; 12} 13.left { 14 background-color: #D3D3D3; 15 padding: 20px; 16 margin: 5px; 17 width: 300px; 18 float:left; 19} 20.right { 21 background-color: #ADFF2F; 22 padding: 20px; 23 margin: 5px; 24 width: 300px; 25 float:right; 26 27} 28</style> 29<script src="http://code.jquery.com/jquery-2.2.4.js"></script> 30<script> 31(function($){ 32 var settings = {}; 33 34 var methods = { 35 init : function( options ) { 36 settings = $.extend({ 37 'uri' : 'ws://localhost:8080', 38 'conn' : null, 39 'message' : '#message', 40 'display' : '#display' 41 }, options); 42 $(settings['message']).keypress( methods['checkEvent'] ); 43 $(this).chat('connect'); 44 }, 45 46 checkEvent : function ( event ) { 47 if (event && event.which == 13) { 48 var message = $(settings['message']).val(); 49 if (message && settings['conn']) { 50 settings['conn'].send(message + ''); 51 $(this).chat('drawText',message,'right'); 52 $(settings['message']).val(''); 53 } 54 } 55 }, 56 57 connect : function () { 58 if (settings['conn'] == null) { 59 settings['conn'] = new WebSocket(settings['uri']); 60 settings['conn'].onopen = methods['onOpen']; 61 settings['conn'].onmessage = methods['onMessage']; 62 settings['conn'].onclose = methods['onClose']; 63 settings['conn'].onerror = methods['onError']; 64 } 65 }, 66 67 onOpen : function ( event ) { 68 $(this).chat('drawText','サーバに接続','left'); 69 }, 70 71 onMessage : function (event) { 72 if (event && event.data) { 73 $(this).chat('drawText',event.data,'left'); 74 } 75 }, 76 77 onError : function(event) { 78 $(this).chat('drawText','エラー発生!','left'); 79 }, 80 81 onClose : function(event) { 82 $(this).chat('drawText','サーバと切断','left'); 83 settings['conn'] = null; 84 setTimeout(methods['connect'], 1000); 85 }, 86 87 drawText : function (message, align='left') { 88 if ( align === 'left' ) { 89 var inner = $('<div class="left"></div>').text(message); 90 } else { 91 var inner = $('<div class="right"></div>').text(message); 92 } 93 var box = $('<div class="box"></div>').html(inner); 94 $('#chat').prepend(box); 95 }, 96 }; // end of methods 97 98 $.fn.chat = function( method ) { 99 if ( methods[method] ) { 100 return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 )); 101 } else if ( typeof method === 'object' || ! method ) { 102 return methods.init.apply( this, arguments ); 103 } else { 104 $.error( 'Method ' + method + ' does not exist' ); 105 } 106 } // end of function 107})( jQuery ); 108 109$(function() { 110 $(this).chat({ 111 'uri':'ws://localhost:8080', 112 'message' : '#message', 113 'display' : '#chat' 114 }); 115}); 116</script> 117</head> 118<body> 119 <input type="text" id="message" size="50" /> 120 <div id="chat" class="container"></div> 121</body> 122</html>
コンソールでのserver.phpが実行中に、index.htmlとしてアクセスすれば問題なく動作します。
しかし、index.htmlからindex.phpに拡張子を変更して、再度アクセスすると読み込み中のまま動作しなくなってしいます。
webサーバーはXAMPPをローカルで起動しております。
コンソールで使用したphp.exeとXAMPPで使用しているphp.exeは同一のものです。
どうすればindex.phpでも動作するようになるのでしょうか。
皆様のご回答をお願いいたします。
追記
動作しないと記入しましたが、読み込み中が永遠と終わらない状態です。
server.phpのスクリプトを停止するとindex.phpがすぐに表示されます。
同じマシン内で、WebサーバーとSocketサーバーが同じマシン内で共存されることは不可能なのでしょうか・・・
回答1件
あなたの回答
tips
プレビュー