0
當我需要socket.io
包時,爲什麼我必須通過整個http
包?socket.io - require(socket.io)(http)
也許是因爲socket.io必須連接到我的http服務器?
這是代碼:
var port = process.env.PORT || 8080;
var express = require("express");
var app = new express();
var http = require("http").Server(app);
var io = require("socket.io")(http);
app.use(express.static(__dirname + "/public"));
// listen for events
io.on("connection", function() { // first arguments is the name of the event
console.log("User connected via socket.io");
})
http.listen(port, function() {
console.log("Server started");
完美!!!!所以我明白瞭如何在服務器上連接socket.io!我學習socket.io ...非常感謝你!非常清楚:) – DevWeb