How are WebSockets used in an html file?


I did this function on an server:

net=new WebSocket("ws://")

And I get an error:


Firefox can not establish a connection to the server at ws: //

Something similar happens in chrome, also if I put this:

<body bgcolor="black">
name=prompt("Enter your name, please.")
net=new WebSocket('ws://')
net.onconnect=function(){net.send(console.log("User: "+name+" has connected"))}
onkeydown=function(){net.send(console.log(prompt("Enter your message.")))}

It's funny, because when I send a message it does not respond. What do I do to make it work?

asked by Cristein 14.10.2016 в 16:10

1 answer


You have to connect to a WS server. No to an HTML file. link

answered by 15.10.2016 в 00:59