I have a Nodejs Server.js code :
first Concept :
var http = require('http');
var express = require('express');
var app = express();
var path = require('path');
var conn= http.createServer(app).listen(3000, function () {
console.log("server Running at Port 3000");
});
var WebSocketServer = require('ws').Server;
var wss = new WebSocketServer({server: conn});
and i have a index.html
code with java script :
<html>
<body>
<script src="myscript.js"></script>
</body>
</html>
inside myscript.js
i have :
var connection = new WebSocket('ws://localhost:3000');
This is working fine when i open http://localhost:3000
on browser .
second Concept :
my server.js
:
var WebSocketServer = require('ws').Server,
wss = new WebSocketServer({ port: 3000}) ;
wss.on('connection', function (connection) {
});
wss.on('listening', function () {
console.log("Server started...");
});
and HTML and client java script is similar as above .
This is not working when i open http://localhost:3000
on browser . why ? i want to clarify my doubt . Why the first method working and second is not working ?