2

I have all of my code on github, so I can edit my code on the fly, and simply do a git pull from the Pi and have my code updated. Right now, I'm trying to make a websocket between my Pi server and my client.

This is my Server.php:

require_once 'WebSockets.php';

class Server extends WebSocketServer {

  protected function process ($user, $message) {
    $this->send($user,$message);
  }
  protected function connected ($user) { }
  protected function closed ($user) { }
}

$server = new Server("localhost", "1738");

try {
    $server->run();
} 
catch (Exception $e) {
    $server->stdout($e->getMessage());
}

and this is my client JS file:

(function() {
    var URL = "ws://192.168.167.28:1738";
    var ws = new WebSocket(URL);

    ws.onopen = function(e) {
        document.getElementById('serverStatus').innerHTML = "Connected";
        document.getElementById('serverStatus').style.color = "green";
    };
    ws.onmessage = function(e) {

    };
    ws.onclose = function(e) {
        document.getElementById('serverStatus').innerHTML = "Not Connected";
        document.getElementById('serverStatus').style.color = "red";
    };
});

What I think might be the problem is the way that I have my www/ directory set up. Currently, it is

www/
    Pi-Room/
       src/
          php/Server.php

and I run Server.php from the command line. Do I need to change the address in my JS to reflect this file structure, or can I try slapping on a .htaccess file which would redirect the user?

1

Short answer, I'd say, is no. If you're using ratchet (or similar), the js will have no knowledge of where the WS server is being hosted from. What you will find though, is that without specifically allowing another domain, you'll need to have the web socket client connecting to the same domain that the HTML is being served from.

Not sure if you've solved this already, but I've been working on a similar project at https://github.com/calcinai/phpi-websocket.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.