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

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

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

CentOSは、主にRed Hat Enterprise Linux(RHEL)をベースにした、フリーのソフトウェアオペレーティングシステムです。

Apache

Apacheは、Apache HTTP Serverの略で、最も人気の高いWebサーバソフトウェアの一つです。安定性が高いオープンソースソフトウェアとして商用サイトから自宅サーバまで、多くのプラットフォーム向けに開発・配布されています。サーバーソフトウェアの不具合(NCSA httpd)を修正するパッチ(a patch)を集積、一つ独立したソフトウェアとして開発されました。

Node.js

Node.jsとはGoogleのV8 JavaScriptエンジンを使用しているサーバーサイドのイベント駆動型プログラムです。

Socket.IO

Socket.IOはNode.js上で動くライブラリであり、すべてのブラウザとモバイルデバイスでリアルタイムのアプリを作動させる事を目的としています。

VirtualBox

VirtualBoxは、現在米オラクル社が開発している、 x86仮想化ソフトウェア・パッケージの一つです。

Q&A

0回答

4785閲覧

Socket.ioがwebsocketのハンドシェイクに失敗する

namaza

総合スコア14

CentOS

CentOSは、主にRed Hat Enterprise Linux(RHEL)をベースにした、フリーのソフトウェアオペレーティングシステムです。

Apache

Apacheは、Apache HTTP Serverの略で、最も人気の高いWebサーバソフトウェアの一つです。安定性が高いオープンソースソフトウェアとして商用サイトから自宅サーバまで、多くのプラットフォーム向けに開発・配布されています。サーバーソフトウェアの不具合(NCSA httpd)を修正するパッチ(a patch)を集積、一つ独立したソフトウェアとして開発されました。

Node.js

Node.jsとはGoogleのV8 JavaScriptエンジンを使用しているサーバーサイドのイベント駆動型プログラムです。

Socket.IO

Socket.IOはNode.js上で動くライブラリであり、すべてのブラウザとモバイルデバイスでリアルタイムのアプリを作動させる事を目的としています。

VirtualBox

VirtualBoxは、現在米オラクル社が開発している、 x86仮想化ソフトウェア・パッケージの一つです。

0グッド

0クリップ

投稿2017/07/06 16:08

編集2017/07/06 16:10

こちらのURLを参考にして、
socket.ioを使うためにApacheでNode.jsをリバースプロキシしています。
サーバはVirtualBox上のCentOS7です。

しかし、接続してもポーリングは行えているようなのですが、websocketになりません。

失敗するのはwsスキーマのURLであるため、
websocketのApacheのリバースプロキシの設定がおかしいのではないかと考えています。

chromeのコンソールの出力

VM1053:35 WebSocket connection to 'ws://192.168.1.23/nodejs/socket.io/?EIO=3&transport=websocket&sid=MVaFdCm29VdTj4fTAAAB' failed: Error during WebSocket handshake: Unexpected response code: 400

httpd.conf(リバースプロキシの設定は最下部にあります。)

httpd.conf

1ServerRoot "/etc/httpd" 2 3Listen 80 4 5Include conf.modules.d/*.conf 6 7User apache 8Group apache 9 10ServerAdmin root@localhost 11 12<Directory /> 13</Directory> 14 15DocumentRoot "/var/www/html" 16 17<Directory "/var/www"> 18 AllowOverride None 19 Require all granted 20</Directory> 21 22<Directory "/var/www/html"> 23 Options Indexes FollowSymLinks 24 AllowOverride All 25 Require all granted 26 Options +ExecCGI 27 AddHandler cgi-script .cgi .pl 28</Directory> 29 30<IfModule dir_module> 31 DirectoryIndex index.html index.cgi index.php 32</IfModule> 33 34<Files ".ht*"> 35 Require all denied 36</Files> 37 38ErrorLog "logs/error_log" 39 40LogLevel warn 41 42<IfModule log_config_module> 43 LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined 44 LogFormat "%h %l %u %t \"%r\" %>s %b" common 45 46 <IfModule logio_module> 47 LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio 48 </IfModule> 49 50 CustomLog "logs/access_log" combined 51</IfModule> 52 53<IfModule alias_module> 54 ScriptAlias /cgi-bin/ "/var/www/cgi-bin/" 55 56</IfModule> 57 58<Directory "/var/www/cgi-bin"> 59 AllowOverride None 60 Options None 61 Require all granted 62</Directory> 63 64<IfModule mime_module> 65 TypesConfig /etc/mime.types 66 67 AddType application/x-compress .Z 68AddType application/x-gzip .gz .tgz 69 70 AddType text/html .shtml 71 AddOutputFilter INCLUDES .shtml 72</IfModule> 73 74AddDefaultCharset UTF-8 75 76<IfModule mime_magic_module> 77 MIMEMagicFile conf/magic 78</IfModule> 79 80EnableSendfile on 81 82IncludeOptional conf.d/*.conf 83 84ServerTokens Prod 85 86KeepAlive On 87 88LoadModule proxy_module lib64/httpd/modules/mod_proxy.so 89LoadModule proxy_http_module lib64/httpd/modules/mod_proxy_http.so 90LoadModule proxy_wstunnel_module lib64/httpd/modules/mod_proxy_wstunnel.so 91 92 RewriteEngine On 93 RewriteCond %{REQUEST_URI} ^/nodejs/socket.io [NC] 94 RewriteCond %{QUERY_STRING} transport=websocket [NC] 95 RewriteRule ^/nodejs(.*) ws://192.168.1.23:3000/$1 [P,L] 96 97ProxyRequests Off 98ProxyPass /nodejs http://192.168.1.23:3000 retry=0 timeout=600 keepalive=On 99#ProxyPassReverse /nodejs ws://192.168.1.23:3000 100ProxyPassReverse /nodejs http://192.168.1.23:3000 101

サーバ側

javascript

1/*jshint esversion: 6 */ 2const http = require( 'http' ); // HTTPモジュール読み込み 3const fs = require( 'fs' ); // ファイル入出力モジュール読み込み 4const server = http.createServer( function( req, res ){ 5 fs.readFile( __dirname + '/index.html', function ( err, data ) { 6 if ( err ) { 7 res.writeHead( 500 ); 8 return res.end( 'Failed to load index.html' ); 9 } 10 res.writeHead( 200 ); 11 res.end(); 12 } ); 13} ).listen( 3000); 14var sock = require('socket.io') 15io = sock.listen(server); 16io.sockets.on('connection', function(socket) { 17 console.log("connection"); 18 socket.on('message', function(data) { 19 console.log('message'); 20 io.sockets.emit('message', {value: data.value}); 21 }); 22 socket.on('disconnect', function() { 23 console.log('disconnect'); 24 }); 25});

クライアント側

javascript

1 2 3<!DOCTYPE html> 4<html lang="ja"> 5<head> 6 <meta charset="UTF-8"> 7 <title>Title</title> 8</head> 9<body> 10<button onclick="sendMessage()" id="buttt" value="bun">bd</button> 11<div id="receivedMessage"></div> 12<script src="nodejs/socket.io/socket.io.js"></script> 13<script type="text/javascript"> 14 var socket = io.connect('http://192.168.1.23', {path:'/nodejs/socket.io'}); 15 socket.on('connect', function(msg) { 16 console.log("connect"); 17 }); 18 socket.on('message', function(msg) { 19 document.getElementById("receivedMessage").innerHTML = msg.value; 20 }); 21 function sendMessage() { 22 var msg = document.getElementById("buttt").value; 23 socket.emit('message', {value: msg}); 24 } 25 function disconnect() { 26 socket.disconnect(); 27 } 28</script> 29</body> 30</html>

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

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

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

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

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

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問