質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.49%
WebSocket

WebSocketとは双方向・全二重コミュニケーションのためのAPIでありプロトコルのことを指します。WebSocketはHTML5に密接に結びついており、多くのウェブブラウザの最新版に導入されています。

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

Q&A

解決済

1回答

790閲覧

WebSocketが実装できない

ryouya

総合スコア14

WebSocket

WebSocketとは双方向・全二重コミュニケーションのためのAPIでありプロトコルのことを指します。WebSocketはHTML5に密接に結びついており、多くのウェブブラウザの最新版に導入されています。

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

0グッド

0クリップ

投稿2021/05/24 23:10

お世話になっております。
下記の問題、知見のある方ご教示お願いいたします。

#起こっている問題
http://challenge.no1s.biz/programming/php/158
上記サイトを参考に、WebSocketの環境を構築し実行したのですが、
うまくいきません。

index.htmlを開いても下記のような画面になってしまいます。
イメージ説明

#確認したこと
参考サイト(http://challenge.no1s.biz/programming/php/158)の指定しているパスを
自分の環境に合わせました。
また、Ratchet等も正常に入っていることを確認いたしました。

#ファイルパス

htdocs Lbin Lchat-server.php Lvendor\cboden\ratchet\src\Ratchet LMessageComponentInterface.php LConnectionInterface.php LHttpServer.php Lvendor\cboden\ratchet\src\Ratchet\Server LIoServer.php Lvendor\cboden\ratchet\src\Ratchet\WebSocket LWsServer.php | autoload.php

#各ファイル
###Chat.php

<?php namespace htdocs; use vendor\cboden\ratchet\src\Ratchet\MessageComponentInterface; use vendor\cboden\ratchet\src\Ratchet\ConnectionInterface; class Chat implements MessageComponentInterface { protected $clients; public function __construct() { $this->clients = new \SplObjectStorage (); } public function onOpen(ConnectionInterface $conn) { // Store the new connection to send messages to later $this->clients->attach ( $conn ); echo "New connection! ({$conn->resourceId})\n"; } public function onMessage(ConnectionInterface $from, $msg) { $numRecv = count ( $this->clients ) - 1; echo sprintf ( 'Connection %d sending message "%s" to %d other connection%s' . "\n", $from->resourceId, $msg, $numRecv, $numRecv == 1 ? '' : 's' ); foreach ( $this->clients as $client ) { if ($from !== $client) { // The sender is not the receiver, send to each client connected $client->send ( $msg ); } } } public function onClose(ConnectionInterface $conn) { // The connection is closed, remove it, as we can no longer send it messages $this->clients->detach ( $conn ); echo "Connection {$conn->resourceId} has disconnected\n"; } public function onError(ConnectionInterface $conn, \Exception $e) { echo "An error has occurred: {$e->getMessage()}\n"; $conn->close (); } }

###chat-server.php

&amp;amp;lt;?php use vendor\cboden\ratchet\src\Ratchet\Server\IoServer; use vendor\cboden\ratchet\src\Ratchet\HttpServer; use vendor\cboden\ratchet\src\Ratchet\WebSocket\WsServer; use htdocs/Chat; require_once 'C:\xampp\htdocs\vendor\autoload.php'; $server = IoServer::factory( new HttpServer( new WsServer( new Chat() ) ), 8080 ); $server-&amp;amp;gt;run();

###autoload.php

<?php // autoload.php @generated by Composer require_once __DIR__ . '/composer/autoload_real.php'; return ComposerAutoloaderInit25e8efa5a00774f32dd34fc959d900f5::getLoader();

###index.html

&amp;amp;lt;html&amp;amp;gt; &amp;amp;lt;script&amp;amp;gt; var conn = new WebSocket('ws://localhost:8080'); conn.onopen = function(e) { console.log("Connection established!"); }; conn.onmessage = function(e) { console.log(e.data); }; &amp;amp;lt;/script&amp;amp;gt; &amp;amp;lt;body&amp;amp;gt; &amp;amp;lt;/body&amp;amp;gt; &amp;amp;lt;/html&amp;amp;gt;

#GitHub

https://github.com/karirin/chat_app

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

m.ts10806

2021/05/25 01:07

htmlをどうやってコピーしました? htmlエンティティになっているという認識はありますか?
ryouya

2021/05/25 04:00

ご確認、ご回答ありがとうございます。 ご指摘の通り、index.htmlの記述に誤りがありました。
ryouya

2021/05/25 04:01

確認いたします。
guest

回答1

0

自己解決

自己解決しました。
index.htmlの記述に誤りがございました。

投稿2021/05/25 10:49

ryouya

総合スコア14

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.49%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問