首先,所有这些都在本地工作。
我正在使用https://github.com/itaylor/redux-socket.io在我的react / redux应用程序的前端启用socket.io。但是,我无法在生产中发出/接收任何事件。
初始连接事件工作正常,我在服务器的日志文件中看到“已连接”。
我已启用套接字调试,并在浏览器的控制台中看到以下消息=> socket.io-client:socket transport is open - connecting +0ms
本地所有事件都被解雇了,但一旦我们转向生产通讯就会崩溃。
本地站点和生产站点之间的唯一区别是,您可以访问/ sub-dir /上的应用程序,在http://localhost上进行本地开发:{PORT} /
我一直在网上实施潜在的修复,但我无法让它工作。
非常感谢任何帮助。如果您需要更多信息,请与我们联系。
我的nginx有以下设置文件:
location /sub-dir/socket.io {
# rewrite ^/sub-dir/socket.io(/.*)$ $1 break;
# rewrite ^/sub-dir/socket.io(/.*)$ $1 break;
proxy_pass http://localhost:3344;
proxy_set_header X-Real-Ip $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
location /sub-dir/ {
rewrite ^/sub-dir(/.*)$ $1 break;
proxy_pass http://localhost:3344;
proxy_set_header X-Real-Ip $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
app.js(我的前端入口点)
import { createStore, compose, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
import routeDispatcher from './routeDispatcher.js';
import reducers from '../reducers';
import createSocketIoMiddleware from 'redux-socket.io';
import io from 'socket.io-client';
import secrets from '../secrets.json';
const socket = io("https://hicarbyecar.com/admin-boards/", {path: '/admin-boards/socket.io', transport: 'websocket'});
const socketIoMiddleware = createSocketIoMiddleware(
socket,
['server/', 'HiBoards::', 'HiSocket::']
);
socket.on('connect', function() {
console.log('whaaat');
socket.emit('Connected');
});
export default function createAppStore() {
return createStore(
reducers,
compose(
applyMiddleware(thunk, routeDispatcher, socketIoMiddleware),
window.devToolsExtension ? window.devToolsExtension() : (f) => f
)
);
}
Server.js
// setting up server
var express = require('express');
var app = express(); // initialising express() to app
var server = require('http').createServer(app);
var io = require('socket.io')(server, {path: '/admin-boards/'}); // io object with server as arg
var port = 3344;
var secrets = require('./secrets.json');
server.listen(port, function() {
console.log('listening to ' + port + ' <<<');
});
var redis = require('redis').createClient;
var redisAdapter = require('socket.io-redis');
var pub = redis(secrets.redis.port, secrets.redis.host, { auth_pass: secrets.redis.pass });
var sub = redis(secrets.redis.port, secrets.redis.host, { auth_pass: secrets.redis.pass });
io.adapter(redisAdapter({ pubClient: pub, subClient: sub }));
// directing app to use files in __dirname
app.use(express.static(__dirname + '/../dist/'));
app.get('/', function(req, res) {});
var mysql = require('mysql');
var connection = mysql.createPool(secrets.db);
// connection of client
io.on('connection', function(socket){
socket.on('Connected', function() {
console.log("Connected Message Received");
socket.emit('Welcome', { message: 'yo' });
});
socket.join('admin-room');
var clients = io.sockets.adapter.rooms['admin-room'].sockets;
console.log(clients);
console.log('connected');
// client disconnected
socket.on('disconnect', function(){ // reseving 'event' and doing function
console.log('... user disconnected');
});
});