SocketCluster / socketcluster-client

JavaScript client for SocketCluster
MIT License
292 stars 91 forks source link

undefined is not an object (evaluating 'global.WebSocket') #8

Closed seiyria closed 9 years ago

seiyria commented 9 years ago

This is probably me doing something dumb, but I'm unable to get this working on the client. What I did:

and include that file.

I've also tried just using the socketcluster.js file from the repo, but that seems to fail when I attempt to use the socket like so:

var socket = window.socketCluster.connect({hostname: 'localhost', port: 8000});
console.log(socket.state); //undefined

And in my server:

module.exports.run = function (worker) {
  var scServer = worker.scServer;

  scServer.on('connection', function (socket) {
    console.log('client connect');

    socket.on('login', function(credentials, respond) {
      console.log('begin login', credentials);
    });

  });
};

But I get nothing in my browser console, except eventually, I get an xhr polling error. Not sure how to get started with this.

jondubois commented 9 years ago

@seiyria In your console do: npm remove -g socketcluster then npm cache clean

Then follow these steps exactly: http://socketcluster.io/#!/docs/getting-started

seiyria commented 9 years ago

I did that exactly, see my project here (it's pretty minimal so far).

jondubois commented 9 years ago

@seiyria Did you run the socketcluster create myappname command afterwards?

seiyria commented 9 years ago

@jondubois No, I ran that to generate the project shown above, then I removed the server / serve-static part of it as this will just be a dedicated backend (the frontend is an app).

jondubois commented 9 years ago

@seiyria So you don't want SC to serve the client file (socketcluster.js) - You want to serve it from somewhere else?

seiyria commented 9 years ago

@jondubois Yes, I was under the impression that it was a client and the client could be served from anywhere else. Based on your reaction, I suspect that won't work so well. I suppose I could serve just the js file from there and have the app include it, but I prefer to not have external assets (I think Apple has a policy against that, so my app would probably get rejected).

jondubois commented 9 years ago

That is possible - You can get the client separately and serve it up from anywhere, but it's a bit trickier to setup (manual). Did you use the socketcluster.js file that is inside socketcluster-client/ repo?

seiyria commented 9 years ago

Yeah, I've tried that as well. My basic code to reproduce was above. It didn't seem to establish a connection.

jondubois commented 9 years ago

@seiyria Note sure if that could explain the problem but I noticed that there was an outdated socketcluster.js client inside the socketcluster/sample/ app - This was an accident - I removed that file from the module on npm. You should only use the one from the https://github.com/TopCloud/socketcluster-client repo.

seiyria commented 9 years ago

Absolutely, that's the one I was using.

jondubois commented 9 years ago

What OS are you using?

seiyria commented 9 years ago

I'm on OSX for dev. Originally, I tried hosting the server on my linux VM on my windows machine across the network, but I eventually moved it all to my OSX machine to eliminate possible network problems as a cause (though once I have something working I will revisit getting that working).

jondubois commented 9 years ago

Is your HTTP server running on the same host/domain as the socketcluster server?

seiyria commented 9 years ago

Yes, currently it is. I'm trying to establish the connection via localhost:8000.

jondubois commented 9 years ago

What do you get when you run socketcluster -v?

seiyria commented 9 years ago

Oh, wait, shoot. Something I'll have to take a look at later, actually, I don't think it is. It's running in the iOS simulator, which simulates a device. localhost would probably refer to that device, not the host machine. That doesn't explain why it couldn't connect when given a specific IP, though.

Sorry, this might be a total non-issue; I didn't even think about that. I'll try to resolve this when I'm at my machine next.

Thanks for your support so far!

jondubois commented 9 years ago

Ok. Let me know. Thanks for reporting. If in doubt, raise an issue :)))

seiyria commented 9 years ago

Hey, so I double checked. Apparently iOS simulator uses the host network, ie, localhost definitely refers to the machine currently running the server.

jondubois commented 9 years ago

So you didn't manage to get it working on any platform? Did you try to run on Windows directly?

It would be interesting to know what's causing this. I might play around with serving the file from a different origin than the SC server just to see what happens - This should be supported by default though.

seiyria commented 9 years ago

I have not (I actually have no development tools available to me on windows by design).

It'd be difficult for me to run it all in just linux, as well, as I was unable to get the android simulator working and I can't get the ios simulator on it, so that unfortunately leaves me stuck on OSX. My project is an Ionic app, I should also add. I don't know if that makes a difference though.

Sure, I mean if it works for you, I'm probably screwing something up, but .. who knows.

jondubois commented 9 years ago

@seiyria I noticed in your first post, you mentioned an 'xhr poll' error - This suggests that you are using version 1.x.x of the client and not the latest 2.x.x version. Could you be using the SC1 client with the SC2 server? The two versions have the same external API but are protocol-incompatible.

Unfortunately, the version number isn't anywhere in the client script (yet) - But to check you can open the socketcluster.js client file and search for the string 'engine.io' - If there are any matches, then it means that you are using v1 of the client.

If that's the issue, make sure you do npm cache clean and then npm install socketcluster-client then get the socketcluster.js file from there - This will be version 2.

npm packages and git branches for SC1 and SC2 have moved around about one or two weeks ago so this could have caused cache issues with npm.

Maybe that's the issue.

seiyria commented 9 years ago

Absolutely, thank you. I will give this a try when I get back and let you know the results. I was out of ideas!

seiyria commented 9 years ago

Ah, I figured out why. I bower installed your repo, but you never pushed tags to it, so I got 0.9. I'll try now and see if it works!

seiyria commented 9 years ago

Yep, and it definitely works now! Thanks for pointing that out. When you attend to #7, make sure you push all your tags :P

Thanks a ton!