使用套接字。io和节点。js、表达和玉石

时间:2022-08-22 15:48:26

I have some trouble to use Socket.io even just to test if a client is connected. I've tried many things and I think that my mistake is, maybe, when I do the app.get function. I have also tried to do this in an route js file but it wasn't conclusive neither. So here are my different codes :

我用插座有点困难。io甚至只是测试客户端是否连接。我尝试了很多东西,我认为我的错误是,当我做app。get函数时。我也尝试在一个route js文件中这样做,但它也不是决定性的。这是我的不同代码:

App.js

App.js

/**
 * Module dependencies.
 */

var express = require('express');
var routes = require('./routes');
var user = require('./routes/user');
var http = require('http');
var path = require('path');
var mongo = require('mongodb');
var monk = require('monk');
var db = monk('mongodb://xxxxx:xxxxx@ds051067.mongolab.com:51067/jdo');

var app = express(), 
    server = http.createServer(app) ,
    io = require('socket.io').listen(server);

// all environments

app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(express.cookieParser('This is secret'));
app.use(express.session());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));

// development only
if ('development' == app.get('env')) {
  app.use(express.errorHandler());
}

io.sockets.on('connection', function (socket) {
    console.log('Un client est connecté !');
});

// app.get('/', routes.index);
app.get('/users', user.list);
app.get('/deplacement',routes.deplacement);
app.get('/monCompte', routes.compte);
app.get('/connexion', routes.connexion);
app.get('/', function(req, res) {
    res.render('index.jade');
});


server.listen(app.get('port'), function(){
  console.log('Express server listening on port ' + app.get('port'));
});

Index.jade

Index.jade

    extends layout

    block content

     script(src="/socket.io/socket.io.js").
      var socket = io.connect('http://localhost:3000');
        });

PS : Sorry if my english is bad ^^

PS:对不起,如果我的英语是坏的^ ^

1 个解决方案

#1


3  

You can't use inline javascript in the same script tag as an included script.

不能在包含脚本的脚本标记中使用内联javascript。

extends layout

block content

script(src="/socket.io/socket.io.js")
script.
    var socket = io.connect('http://localhost:3000');

#1


3  

You can't use inline javascript in the same script tag as an included script.

不能在包含脚本的脚本标记中使用内联javascript。

extends layout

block content

script(src="/socket.io/socket.io.js")
script.
    var socket = io.connect('http://localhost:3000');