こちらの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>
あなたの回答
tips
プレビュー