不要找socket.io

时间:2016-10-03 16:58:29

标签: javascript node.js socket.io

档案

chat
    node_modules
    web
       public
          css
             estilo.css
          img
             tiempo.jpg
          js
             script.js
          view
             vista.html
       app.js
       package.json

JSON

{
    "name":"chat",
    "version":"0.0.1",
    "privte":"true",
    "dependencies":{
        "socket.io":"1.4.8",
        "express": "4.14.0"
    }   
}

在文件 vista.html 中我无法打开或加载soket.io,但我可以加载文件:pictures,css,js - 没有问题。问题是尝试加载 soket.io

NODE.js文件, app.js

var express = require("express"),
    app = express(),
    http = require("http").Server(app), 
    io = require("socket.io")(http), 
    nicknames=[],
    users={};


app.listen(3000, function () {
  console.log('SERVIDOR LISTO');
});


app.use(express.static(__dirname + '/web/public'));

app.get("/",function(req, res){
        res.sendFile( __dirname+'/web/public/view/index.html');
});

io.sockets.on("connection", function(socket){ 
    .......
    .......
}

我的html文件是

<html>
<head>
    <title></title>
    <script src="/socket.io/socket.io.js"></script>
</head>
<body> 
    <img src="http://localhost:3000/img/tiempo.jpg" 
     alt="Smiley face" height="30%" width="30%">
</body>
</html>

1 个答案:

答案 0 :(得分:0)

我找到了解决问题的方法。我不得不重新排序代码,首先开始,然后用最后一个完成。

var express = require('express'),
    app     = express(),
    io      = require('socket.io'),
    nicknames=[],
    users={}; 

var server=app.listen(3000, function () {
  console.log('SERVIDOR LISTO');
});

var mysocket  = io.listen(server);  
app.use(express.static(__dirname + '/web/public'));

app.get("/",function(req, res){
        res.sendFile( __dirname+'/web/public/view/index.html');
});