我的客户端socket.io在哪里? [英] Where is my client side socket.io?

查看:106
本文介绍了我的客户端socket.io在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用Express和React-Engine创建了一个同构的React应用程序。现在我正在尝试连接socket.io。在快速设置我有

I have created an isomorphic React app using Express and React-Engine. Right now I am trying to connect socket.io. In the express setup I have

var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
...
app.set('views', __dirname + '/public/views');

app.get('/', function(req, res) {
  res.render(req.url, {
    title: 'React Engine Express Sample App',
    name: 'something'
  });
});
...
var server = app.listen(3000);

io.on('connection', function (socket) {
  socket.on('logIn', function (data) {
      console.log(data);     
  });
});

在前端我有一个React组件布局

On the front end I have a React component Layout

var React = require('react');
var socket;

module.exports = React.createClass({
    componentDidMount: function () {       
        socket = io();        
    },
    clicked: function () {
        socket.emit('logIn', {name: "Dan"});    
        console.log("clicked");
    },
    render: function render() {
        return (
          <html>
            <body>
              {this.props.children}
              <button onClick={this.clicked}>click me</button>
              <script src="/socket.io/socket.io.js"></script>
            <script src='/bundle.js'></script>
            </body>            
          </html>
        );
    }
});

项目结构是

index.js(express app)
package.json
/public
    bundle.js(generated by webpack)
    index.js(front-end routing setup)
    routes.jsx
    /views
        app.jsx
        layout.jsx(where the front end really starts)

从我看到的一切都应该有效。 Express应用程序应该托管socket.io文件,以便我可以在前端抓住它。尽管这个也不是localhost:3000 / socket.io / socket.io.js都有效。我甚至尝试了几个不同的相对路径。我如何找到我的客户端socket.io文件?

From everything I have seen that should work. The express app should host the socket.io files so that I can grab it on the front end. Despite that neither this nor localhost:3000/socket.io/socket.io.js have worked. I have even tried a few different relative paths. How can I find my client side socket.io files?

推荐答案

我使用的顺序是这样的:

The sequence I am using that works is this:

var express = require('express');
var app = express();
var server = app.listen(8081);
var io = require('socket.io').listen(server);

我不知道您的哪个方面可能导致问题,但与此不同,特别是在如何设置服务器(你分配两个不同的值到你的服务器变量似乎不正确)以及如何初始化socket.io。

I'm not sure exactly which aspect of yours might be causing the problem, but it's different than this, particularly in how you set up the server (you assign two different values to your server variable which does not seem right) and how you initialize socket.io.

此外,我看到一些客户端版本的socket.io安装在服务器端,而不是服务器版本显然不会给你服务/套接字.io / socket.io.js文件,以便值得检查。

Also, I've seen some people with the client version of socket.io installed server-side, not the server version which obviously won't give you the ability to serve the /socket.io/socket.io.js file so that's worth checking.

这篇关于我的客户端socket.io在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆