Почему не работает socket.io?

Есть такой серверный код на CoffeeScript:
### Config section ###

config =
    version : "0.0.1"
    port    : 3000



### Require section ###

console    = require "better-console"
express    = require "express"
fs         = require "fs"
http       = require "http"
mustache   = require "mustache"
nodeStatic = require "node-static"
socketio   = require "socket.io"



### App initializing section ###

app    = express()
server = http.Server app
io     = socketio http



### Node static initializing section ###

file = new nodeStatic.Server '.'



### Mustache initializing section ###

render = mustache.render

renderFile = (path, data) ->
    render (fs.readSync fs.openSync(path, "r"), 999999)[0], data



### Main section ###

app.get '/', (req, res) ->
    data =
        vchat:
            version: config.version
        user:
            name: "Guest"

    res.send renderFile "templates/index.html", data
    return


app.get "/static/*", (req, res) ->
    file.serve req, res
    return

io.on "connection", (socket) ->
    console.log "a user connected"
    return



### Listening section ###

server.listen config.port, ->
    console.clear()
    console.info render(
        '''===========================
        Server running on port {{port}}
        ===========================''',
        config
    )
    return

(см. Require section, App initializing section, Main section, Listening section)

Такой index.html:
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <title> ... </title>

        <!-- Socket.io -->
        <script src="../static/socket.io.js"></script>

        <!-- App -->
        <link rel="stylesheet" href="../static/style.css"/>
        <script src="../static/script.js" defer></script>
    </head>
    <body> ... </body>
</html>


И такой script.js (подключается в index.html):
var socket = io();

В консоли Chrome вижу следующее:
647ac8f22e8c40a88e3cb3e6bf2b4304.png

При этом функция, переданная в io.on, не выполняется (вывода "a user connected" в консоль на сервере нет)

чяднт?
  • Вопрос задан
  • 663 просмотра
Решения вопроса 1
ILE-Salim
@ILE-Salim
Web-developer
Сделайте вот так:
io = socketio(server); 
а не io = socketio(http);
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы