小编Ann*_*ker的帖子

Socket.io 连接总是假的

我的socket.connected总是false,也不能发出或接收消息。

应用程序.js

var app = require('./config/server');    
var http = require('http').Server(app);
var io = require("socket.io")(http);

http.listen(80, function(err)
{
    console.log(err);
    console.log('Server client instagram_clone_v01 online');
});

io.sockets.on('connection', function (socket)
{
    console.log("new user connected");
});
Run Code Online (Sandbox Code Playgroud)

服务器端

var sockets = io();
sockets.on('connection', function ()
{
    console.log("connected");
    sockets.emit("newPhoto");
});
Run Code Online (Sandbox Code Playgroud)

客户端

const socket = io.connect("http://localhost:80");
console.log(socket.connected);

socket.on('error', function()
{
    console.log("Sorry, there seems to be an issue with the connection!");
});

socket.on('connect_error', function(err)
{
    console.log("connect failed"+err);
});

socket.on('connection', function ()
{
    console.log("connected");
    socket.on('newPhoto',function()
    { …
Run Code Online (Sandbox Code Playgroud)

javascript sockets node.js socket.io

5
推荐指数
1
解决办法
6240
查看次数

标签 统计

javascript ×1

node.js ×1

socket.io ×1

sockets ×1