2017-08-25 74 views
0

我想要遵循本教程:https://www.simonewebdesign.it/101-web-socket-protocol-handshake/开发简单的websocket协议。无法创建简单的websocket - NodeJS

我参观localhost:1337/index.html但我得到:

This localhost page can’t be found

No web page was found for the web address: http://localhost:1337/index.html Search Google for localhost 1337 index HTTP ERROR 404

如果我访问这个网址:file:///C:/Users/.../websocket-demo/index.html

我ATLEAST看到所呈现的index.html页面。但在控制台我得到这个错误:

WebSocket connection to 'ws://localhost:1337/' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED

我不知道什么是错?

我有3个文件:index.htmlserver.jsclient.js

server.js

#!/usr/bin/env node 
var WebSocketServer = require('websocket').server; 
var http = require('http'); 

var server = http.createServer(function(request, response) { 
    console.log('Received request from ' + request.url); 
    response.writeHead(404); 
    response.end(); 
}); 

server.listen(1337, function() { 
    console.log('Server is listening on port 1337.'); 
}); 

wsServer = new WebSocketServer({ 
    httpServer: server, 
    autoAcceptConnections: false // because security matters 
}); 

function isAllowedOrigin(origin) { 
    console.log('Connection requested from origin ' + origin); 

    valid_origins = [ 
    'http://localhost:8080', 
    '127.0.0.1', 
    'null' 
    ]; 

    if (valid_origins.indexOf(origin) != -1) { 
    console.log('Connection accepted from origin ' + origin); 
    return true; 
    } 

    console.log('Origin ' + origin + ' is not allowed.') 
    return false; 
} 

wsServer.on('connection', function(webSocketConnection) { 
    console.log('Connection started.'); 
}); 

wsServer.on('request', function(request) { 

    var connection = isAllowedOrigin(request.origin) ? 
    request.accept('echo-protocol', request.origin) 
    : request.reject(); 

    connection.on('message', function(message) { 

    var response = ''; 
    console.log('Received Message: ' + message.utf8Data); 

    if (message.type === 'utf8') { 

     switch (message.utf8Data) { 
     case 'hi': 
      response = 'Hey there'; 
      break; 
     case 'hello': 
      response = 'Heya!'; 
      break; 
     case 'xyzzy': 
      response = 'Nothing happens.'; 
      break; 
     case 'desu': 
      response = 'Keep typing, man. Keep typing.'; 
      break; 
     default: 
      response = "Hello. Uh... what am I supposed to do with '" + 
      message.utf8Data + "'?"; 
     } 
     connection.sendUTF(response); 
    } 
    }); 
    connection.on('close', function(reasonCode, description) { 
     console.log(connection.remoteAddress + ' has been disconnected.'); 
    }); 
}); 

client.js

(function() { 

    var ws = new WebSocket('ws://localhost:1337', 'echo-protocol'); 

    ws.onopen = function (event) { 
    console.log('Connection opened.'); 
    } 

    ws.onmessage = function (event) { 
    console.log('Response from server: ' + event.data); 
    } 

    ws.onclose = function (event) { 
    console.log('Connection closed.'); 
    } 

    ws.onerror = function (event) { 
    console.log('An error occurred. Sorry for that.'); 
    } 

    WebSocket.prototype.sendMessage = function (message) { 
    this.send(message); 
    console.log('Message sent: ' + message); 
    } 

    document.getElementById('send').addEventListener('click', function (event) { 
    event.preventDefault(); 
    var message = document.getElementById('message').value; 
    ws.sendMessage(message); 
    }); 

})(); 

index.html的 - 包括形式

<!doctype html> 
<html> 
<head> 
    <meta charset="utf-8"> 
    <title>WebSocket Client Demo</title> 
</head> 
<body> 

    <h1>WebSocket Client</h1> 

    <form> 
    <label for="message">Send a message</label> 
    <input id="message" name="message" type="text"> 
    <button id="send" name="send">Send</button> 
    </form> 

    <script src="client.js"></script> 
</body> 
</html> 

回答

2

您的网络服务器不能为您的index.html文件提供服务。

你可以看到this post了解如何提供静态文件,或者你可以启动另一个HTTP服务器来为你的索引文件提供服务,就像使用python一样,他们在教程的README文件中建议你如下:https://github.com/simonewebdesign/websocket-demo