I want to use Websocket. For this, I used this web site websocket.
I installed Node.js by
curl -sL http://ift.tt/1oa11OK | sudo bash -
sudo apt-get install -y nodejs
and I installed ws by
npm install ws
in my file server.html I have:
<html>
<head>
<script data-main="main" src="require.js"></script>
</head>
<body>
</body>
</html>
and in my main.js I have:
var WebSocketServer = require('ws').Server
, wss = new WebSocketServer({ port: 8080 });
wss.on('connection', function connection(ws) {
ws.on('message', function incoming(message) {
console.log('received: %s', message);
});
ws.send('something');
});
the folder node_modules for ws is in the same directory as the file require.js.
The problem when I type in the browser http://my_ip/server.html, The websocket is not created and the console dispaly this error: Module name 'ws' has not been loaded yet for context _. use require( ).
Anyone know how to fixe this? I guess that JS is asynchronous, which means it continue executing the code without waiting loading file to be finished.
Thanks for reply
Aucun commentaire:
Enregistrer un commentaire