Reputation: 57
I am currently using a raspberry pi (RPI) with LAMP to host my webpage on my local network which uses websocket to stream some data representing the state of an LED. My problem is that, upon trying to establish the websocket connection, I get the following error:
WebSocket connection to 'ws://raspberrypi:8080/' failed: Error in connection establishment: net::ERR_NAME_NOT_RESOLVED
I believe my error is just due to incorrect URL but I did not find another example of how to solve this error in my research.
This error occurs when I try to establish a connection on my laptop over LAN. If I go to my browser on my raspberry pi and try to establish the websocket, this error does not occur. So does this mean that perhaps my server (apache2) is getting in the way?
Here is my Javascript file for the client:
addEventListener('load',init);
function init() {
console.log('page ready');
var ws = new WebSocket('ws://raspberrypi:8080/');
console.log(ws);
}
Here is my python script which outputs the data I want to stream:
from gpiozero import LED
import time
import sys
if __name__ == "__main__":
led = LED(3);
while True:
led.on()
print 1
sys.stdout.flush() #flush: print to screen immediately
time.sleep(1.5)
led.off()
print 0
sys.stdout.flush() #flush: print to screen immediately
time.sleep(1.5)
And here is the terminal feedback from establishing the websocket on the pi: snapshot
Upvotes: 1
Views: 1681
Reputation: 57
Turns out Apache is a http server, and cannot support websockets (without 3rd party software). I have now successfully set up a websocket using Flask-SocketIO.
Upvotes: 1
Reputation: 104
This looks like a sheer dns issue. You might want to edit your hosts file (under linux it's usually /etc/hosts, under windows it should be c:\windows\system32\drivers\etc) to include raspberrypi's ip. Something like:
raspberrypi 192.168.0.20
where the ip address is your raspberry's ip
Upvotes: 0