我使用ws
库。
代码:
WebSocket = require("ws");
ws = new WebSocket("ws://echo.websocket.org");
ws.onopen = function() {
ws.send("Hello, world!");
};
ws.onmessage = function(msg) {
console.log("Message: " + msg.data);
};
ws.onerror = function(e) {
console.log(e);
};
ws.onclose = function() {
console.log("Closed");
};
控制台返回:
{ [Error: getaddrinfo ENOTFOUND echo.websocket.org echo.websocket.org:80]
code: 'ENOTFOUND',
errno: 'ENOTFOUND',
syscall: 'getaddrinfo',
hostname: 'echo.websocket.org',
host: 'echo.websocket.org',
port: 80,
type: 'error',
target:
WebSocket {
domain: null,
_events:
{ open: [Object],
message: [Object],
error: [Object],
close: [Object] },
_eventsCount: 4,
_maxListeners: undefined,
_socket: null,
_ultron: null,
_closeReceived: false,
bytesReceived: 0,
readyState: 0,
supports: { binary: true },
extensions: {},
_isServer: false,
url: 'ws://echo.websocket.org',
protocolVersion: 13 } }
昨天起作用了,但今天不起作用。我试着重新安装Node.JS
和ws
,但没有帮助。我该怎么修?
我运行了cmd.exe
和ping echo.websocket.org
。然后我再次尝试运行我的脚本。我不知道为什么,但它起了作用。可能是Node.JS DNS的一个错误。