简体   繁体   English

将socket.io与Kraken.js一起使用

[英]Use socket.io with Kraken.js

I have difficulties to work with socket.io, I thought my project used modules which could enter in confilct with socket.io, so I started a new KrakenJs project, and installed just socket.io. 我在使用socket.io时遇到了困难,我以为我的项目使用的模块可以与socket.io配合使用,因此我开始了一个新的KrakenJs项目,仅安装了socket.io。

As the examples found in the official socket.io website, I tried this : 作为在socket.io官方网站上找到的示例,我尝试了以下操作:

//index.js
'use strict';


var kraken = require('kraken-js'),
    app = require('express')(),
    server = require('http').Server(app),
    io = require('socket.io')(server),
    options = {
        onconfig: function (config, next) {
            //any config setup/overrides here
            next(null, config);
        }
    },
    port = process.env.PORT || 8000;


app.use(kraken(options));

io.on('connection', function(socket){
  console.log('a user connected');
});


app.listen(port, function (err) {
    console.log('[%s] Listening on http://localhost:%d', app.settings.env, port);
});

My route controller : 我的路线控制器:

'use strict';
module.exports = function (router) {
    router.get('/', function (req, res) {
        res.sendfile('./public/templates/index.html');
    });
};

And the html page (the same as the example in the website) : 和html页面(与网站中的示例相同):

<!doctype html>
<html>
  <head>
    <title>Socket.IO chat</title>
    <style>
      * { margin: 0; padding: 0; box-sizing: border-box; }
      body { font: 13px Helvetica, Arial; }
      form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }
      form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; }
      form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; }
      #messages { list-style-type: none; margin: 0; padding: 0; }
      #messages li { padding: 5px 10px; }
      #messages li:nth-child(odd) { background: #eee; }
    </style>
  </head>
  <body>
    <ul id="messages"></ul>
    <form action="">
      <input id="m" autocomplete="off" /><button>Send</button>
    </form>
   <script src="/socket.io/socket.io.js"></script>
  <script>
    var socket = io();
  </script>
  </body>

</html>

With that code, I have the same error than in my other kraken app... So I decided to test a simple node app without kraken framework, and works fine. 使用该代码,我的错误与其他kraken应用程序中的错误相同。因此,我决定测试一个没有kraken框架的简单节点应用程序,并且运行良好。

Here's the error which appears with krakenJs : 这是krakenJs出现的错误:

在此处输入图片说明

Where does it come from ? 它从何而来 ? I also test with and without requireJs, but doesn't affect... 我也可以在有和没有requireJs的情况下进行测试,但不会影响...

EDIT : I saw the error is visible when I enter the client-side script before calling require.js : 编辑:当我在调用require.js之前输入客户端脚本时,看到的错误是可见的:

<script src="/socket.io/socket.io.js"></script>
<script>
  var socket = io();
</script>
<script data-main="/js/app" src="/components/requirejs/require.js"></script>

And if I put script after : 如果我在下面加上脚本:

<script data-main="/js/app" src="/components/requirejs/require.js"></script>
<script src="/socket.io/socket.io.js"></script>
<script>
  var socket = io();
</script>

...there's no errors, but the script is not used (no "connected" message in the console). ...没有错误,但是没有使用脚本(控制台中没有“已连接”消息)。

Any idea ? 任何想法 ?

I finally found what was the problem. 我终于找到了问题所在。

In fact, socket.io has to initialize its program on the server to use inside it. 实际上,socket.io必须在服务器上初始化其程序才能在其中使用。 But when you use : 但是当您使用时:

var server = require('http').Server(app),
    io = require('socket.io')(server);

You are using ther server defined by the server variable. 您正在使用server变量定义的server So you have to use the listen() method on this variable. 因此,您必须在此变量上使用listen()方法。

server.listen(8080);

But, by using this variable, I don't think Kraken will be well used in the application. 但是,通过使用此变量,我认为Kraken无法在应用程序中很好地使用。 Unfortunately, I didn't find any possibilities to use directly the app variable for listen() method. 不幸的是,我没有发现直接将app变量用于listen()方法的任何可能性。

You can try this 你可以试试这个

app.listen(process.env.PORT, function () {
    console.info('Server listening on http://localhost:%d', this.address().port);
    io = require('socket.io')(this);
});

and give the connection functions at start event function 并在启动事件功能中提供连接功能

app.on('start', function () {
    console.info('Application ready to serve requests.');
    console.info('Environment: %s', app.kraken.get('env:env'));

    io.on('connection', function (socket) {
        console.log('io connection');
        socket.emit('news', {hello: 'world'});
        socket.on('my other event', function (data) {
            console.log(data);
        });
    });

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM