I have created an instance of a web socket using NodeJS in a file called app.js and declared a listener on port 8080. However, when I run the command to start the connection- the connection is not being established and is not logging to console. I am trying to open a web socket connection and create a talking server. When I run my app.js- the createServer() function doesn't fire and "new connection" is not being logged. Are there more parameters that I need in order to open the connection? I am new to websockets; any help is appreciated.
Here is my app.js code:
var ws = require("nodejs-websocket")
console.log("testing")
var server = ws.createServer(function (conn) {
console.log("New connection")
conn.on("text", function (str) {
console.log("Received "+str)
conn.sendText(str.toUpperCase()+"!!!")
})
conn.on("close", function (code, reason) {
console.log("Connection closed")
})
}).listen(8080);
Here is where I try to connect to the websocket in the client site. I would like to send the websocket whatever is inputted into the text box and then log the return string in the input tag.
<html>
<body>
<p>
Result: <output type=text id="result0" value="" readonly></output>
</p>
<input type="text" onchange="connection.send(this.value); " />
<script>
var connection = new WebSocket("ws://localhost:8080/echo");
connection.onmessage = function (event){
document.getElementById("result0").value = event.data;
};
</script>
</body>
</html>
via user3424216
No comments:
Post a Comment