【问题标题】:how to use express and socket.io如何使用 express 和 socket.io
【发布时间】:2014-04-24 12:34:30
【问题描述】:

我正在使用 express V 3.4.8 和 socket.io V 0.9.16 来创建一个简单的应用程序,该应用程序可以渲染一个带有标记的地图,这些标记放置在人们连接到站点的位置,所有这些都是为了学习 node.js 和使用地图。我的问题是我无法让 socket.io 工作,因为从未交付,更糟糕的是:

这是我的服务器javascript:

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


app.configure(function(){
    app.use('/', express.static(__dirname + '/public'));
    app.use(express.bodyParser());
    app.use(express.methodOverride());
    app.use(app.router);
});

app.configure('development', function(){
  app.use(express.errorHandler());
});


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

server.listen(3000, 'localhost');


// console.log('Listening on port %d', server.address().port);

我从控制台获取的日志是:

info: socket.io started
info: unhandled socket.io url

这是我的 google 的 chrome 日志:

Uncaught ReferenceError: io is not defined (index):21
Resource interpreted as Script but transferred with MIME type text/plain:"http://localhost:3000/socket.io.js". (index):36
io is not defined

所以我认为问题在于快递管理传递文件的方式,但是当我学习时,我不太了解后门发生了什么。我至于有人可以帮助我理解为什么 socket.io.js 没有被交付,我该如何解决这个问题。感谢你们的帮助!。

** 编辑 ** 我在项目中的文件结构也类似于:

mapApp
 -node_modules
 -public
   -css
   -img
   -js
   index.html
 app.js (which is the server)

这也是我的客户端js:

$(function(){

    // create a map in the "map" div, set the view to a given place and zoom
    var map = L.map('map',{
        center:[0,0],
        zoom:2,
        minzoom:2,
        maxZoom:18
    });

    // add an OpenStreetMap tile layer
    L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
        attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
    }).addTo(map);

    /* find user */
    map.locate({setView:true, maxZoom:16});

});

/* client socket*/
/* socket */
var socket = io.connect('http://localhost:3000');
    socket.on('news', function (data) {
    console.log(data);
    socket.emit('my other event', { my: 'data' });
}); 

我的 index.html:

<!DOCTYPE html>
<!--[if lt IE 7]>      <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]>         <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>         <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <title>mapApp</title>
        <meta name="description" content="">
        <meta name="viewport" content="width=device-width">

        <link rel="stylesheet" href="css/normalize.min.css">
        <link rel="stylesheet" href="css/leaflet.css">
        <link rel="stylesheet" href="css/basic.css">
        <link rel="stylesheet" href="css/main.css">

        <script src="js/vendor/modernizr-2.6.2.min.js"></script>
    </head>
    <body>

        <h1>The map</h1>
        <div id="map"></div>

        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
        <script>window.jQuery || document.write('<script src="js/vendor/jquery-1.10.1.min.js"><\/script>')</script>

        <script src="/socket.io.js"></script>
        <script src="js/vendor/leaflet.js"></script>
        <script src="js/main.js"></script>
    </body>
</html> 

【问题讨论】:

    标签: javascript node.js sockets express socket.io


    【解决方案1】:

    您必须在服务器端代码中添加路由处理程序:

    app.get('/', function(req, res){
       res.sendFile('index.html');
    });
    

    index.html 将拥有您的客户端套接字代码。它必须导入 clent 端的 socket.io 库。

    <script src='/socket.io/socket.io.js' />
    <script>
        var socket = io.connect('http://127.0.0.1:3000');
        socket.on('news', function (data) {
            console.log(data.msg);
        });
    </script>
    

    【讨论】:

    • 这是处理以 index.html app.use('/', express.static(__dirname + '/public')); 的形式提供我的静态 html。这让我认为只有在公共内部传递文件?...那我怎么能传递 socket.io 呢?
    • @JhonnatanGonzalezRodriguez 您能否也将您的客户端代码发布到问题中?
    • @AmalAntony 方法名称是 res.sendfile() 而不是 res.sendFile()
    猜你喜欢
    • 2014-10-20
    • 2014-07-06
    • 2018-04-04
    • 2012-10-17
    • 2018-03-07
    • 2015-02-28
    • 1970-01-01
    • 1970-01-01
    • 2022-01-11
    相关资源
    最近更新 更多