简体   繁体   English

无法让 socket.io 客户端连接到服务器

[英]Can't get socket.io client to connect to server

I am trying to connect a client to a server using express and ran with node.js.我正在尝试使用 express 将客户端连接到服务器并使用 node.js 运行。

It would seem I am unable to connect to the server.我似乎无法连接到服务器。 The guide( https://socket.io/get-started/chat ) I followed had the server sending the html page to the client (localhost:3000).我遵循的指南( https://socket.io/get-started/chat )让服务器将 html 页面发送到客户端(localhost:3000)。 I am trying to run the html by double-clicking on a file, but it cannot connect to the running server.我试图通过双击文件来运行 html,但它无法连接到正在运行的服务器。 I am quite new to this, and any help would be appreciated.我对此很陌生,任何帮助将不胜感激。

Every 3 or so seconds, this pops up in the browser console:每隔 3 秒左右,就会在浏览器控制台中弹出:

Error: GET http://file/socket.io/?EIO=3&transport=polling&t=NM2_lBl net::ERR_NAME_NOT_RESOLVED
index.js:83

The index.js file referenced is not written by me., but the line is引用的 index.js 文件不是我写的,但那行是

Backoff.prototype.setJitter = function(jitter){
  this.jitter = jitter;
};

Here is my code:这是我的代码:

My client code is:我的客户端代码是:

var socket = io();
console.log(socket)

function sendMe() {
    socket.emit('data update', me)
}

socket.on('data', function (msg) {
    console.log("test")
    if (msg != null) {
        var real = JSON.parse(msg)
        pUps = real;
    }
});

Any my server code:任何我的服务器代码:

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

var characters = [];
var pUps = [];

app.get('/', (req, res) => {
    console.log("Got here")
    res.send('<h1>Hello world</h1>');
});

io.on('connection', (socket) => {
    console.log("connection")
    socket.on('data update', (msg) => {
        //process data
    });
});

http.listen(3000, () => {
    console.log('listening on *:3000');
});

I am trying to run the html by double-clicking on a file, but it cannot connect to the running server.我试图通过双击文件来运行 html,但它无法连接到正在运行的服务器。

This will not work.这是行不通的。 You have to access the HTML file using http:// or https:// , not by opening a local file.您必须使用http://https://访问 HTML 文件,而不是通过打开本地文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM