tag:blogger.com,1999:blog-4350479895872803738.post9044306340846654474..comments2024-03-26T00:29:46.821-07:00Comments on Navarr Barnier: How to create a socket server in PHPAnonymoushttp://www.blogger.com/profile/06358830143045709829noreply@blogger.comBlogger35125tag:blogger.com,1999:blog-4350479895872803738.post-84433313047245930722012-02-06T19:41:00.000-08:002012-02-06T19:41:00.000-08:00it should recognize and clear up disconnected sock...it should recognize and clear up disconnected sockets. I will probably need to do more<br /> testing before I can get back to you with a proper answer.<br /> Navarr Barnierhttp://tech.gtaero.net/noreply@blogger.comtag:blogger.com,1999:blog-4350479895872803738.post-42498224056700420582012-02-06T19:39:00.000-08:002012-02-06T19:39:00.000-08:00Hello again, as I said before this is a masterpiec...Hello again, as I said before this is a masterpiece, unfortunatelly I've been testing<br /> as a socket server whit several devices connecting by GPRS, the connection is fine, but after a while the<br /> server doesn't seem to recognize that the device have changes socket, for some reason I think it could be a<br /> problem with the phone company and it's APN. <br><br>I have only 6 devices and almost 1000<br /> sockets opened. Have you experienced something like this before? Do I have to implement a system like a<br /> heartbeat to recognize the disconnection?<br><br>Any advice is very welcome. I will keep<br /> working on it, and I'll post re results.<br><br>Thanks anyway!Mackernoreply@blogger.comtag:blogger.com,1999:blog-4350479895872803738.post-26585198161406269702011-01-16T11:03:00.000-08:002011-01-16T11:03:00.000-08:00Out of interest, what are the limitations as far a...Out of interest, what are the limitations as far as the max_clients is concerned?<br /> Racheljones1973noreply@blogger.comtag:blogger.com,1999:blog-4350479895872803738.post-31813258022590217142011-01-16T14:46:00.000-08:002011-01-16T14:46:00.000-08:00As much as your system can handle I suppose. I hav...As much as your system can handle I suppose. I haven't really tested it, nor do I know<br /> PHP well enough to say.Navarr Barnierhttp://tech.gtaero.net/noreply@blogger.comtag:blogger.com,1999:blog-4350479895872803738.post-76960928337659389302012-02-16T17:37:00.000-08:002012-02-16T17:37:00.000-08:00i experience this error too, any help?i experience this error too, any help?Sledgehammer23401noreply@blogger.comtag:blogger.com,1999:blog-4350479895872803738.post-69603290024945595882012-02-04T10:39:00.000-08:002012-02-04T10:39:00.000-08:00This is a masterpiece, after many days searching o...This is a masterpiece, after many days searching on google and many days struggling<br /> with socket servers, I've found you! and your wonderful code.<br><br>This is definetly a great<br /> job, thank you very much for making my day man.Mackernoreply@blogger.comtag:blogger.com,1999:blog-4350479895872803738.post-27764201520641996442012-02-04T17:37:00.000-08:002012-02-04T17:37:00.000-08:00I'm glad you liked it! If you have any bugs o...I'm glad you liked it! If you have any bugs or any improvements to make with it,<br /> please feel free to put in a bug report or fork it on github at <br>https://github.com/navarr/PHP-SocketServerNavarr Barnierhttp://tech.gtaero.net/noreply@blogger.comtag:blogger.com,1999:blog-4350479895872803738.post-35822110646112398382011-10-31T09:09:00.000-07:002011-10-31T09:09:00.000-07:00Hi, Navarr -Thank you for taking the time to put t...Hi, Navarr -<br><br>Thank you for taking the time to put this code<br /> together!<br><br>I have a question for you about this line in SocketServer::loop_once():<br> <br /> $this->trigger_hooks("CONNECT",$this->clients[$i],"");<br><br>I'm assuming that<br /> "CONNECT" is a method that needs development. Is this correct?<br><br>_______________________<br><br>I<br /> have another question for you in regards to JavaScript. Do you think JavaScript frameworks that abstract<br /> developers away from HTML/CSS will end up being the future of web development?Xaviernoreply@blogger.comtag:blogger.com,1999:blog-4350479895872803738.post-87826168315377253782011-10-31T09:32:00.000-07:002011-10-31T09:32:00.000-07:00If you're asking what I think you're askin...If you're asking what I think you're asking, no. "CONNECT" is the name of the hook<br /> being triggered.<br><br>When using the class, you attach a method to that hook with $server->hook("CONNECT","method_name");_______________________As<br /> for the JavaScript bit.. I think it has an important place, but won't be the future.Navarr Barnierhttp://tech.gtaero.net/noreply@blogger.comtag:blogger.com,1999:blog-4350479895872803738.post-36244734751633353122012-02-16T18:07:00.000-08:002012-02-16T18:07:00.000-08:00This seems to be a general issue. I'm not pass...This seems to be a general issue. I'm not passing it as a reference. Its only a warning<br /> so it can be ignored. Additionally, you can remove the ampersand from the first parameter of handle_connect.Navarr Barnierhttp://tech.gtaero.net/noreply@blogger.comtag:blogger.com,1999:blog-4350479895872803738.post-9493268781368107532012-01-13T08:17:00.000-08:002012-01-13T08:17:00.000-08:00with this example i get this if a client connects:...with this example i get this if a client connects:<br>PHP Warning: Parameter 1<br /> to handle_connect() expected to be a reference, value given in /var/rba/socketserver.class.php on line 193Tobias Herkulanoreply@blogger.comtag:blogger.com,1999:blog-4350479895872803738.post-70163684452500883972012-04-10T02:57:00.000-07:002012-04-10T02:57:00.000-07:00Wicked bro!After proper check on out it's work...Wicked bro!<br>After proper check on out it's working, and some tuning to add<br /> some auth form for connected clients, and a couple of more treats, its been a solid php socks class so<br /> far... <br><br>nice.undertuganoreply@blogger.comtag:blogger.com,1999:blog-4350479895872803738.post-83706735659023838252012-04-28T13:43:00.000-07:002012-04-28T13:43:00.000-07:00This program is a server in itself. You'd star...This program is a server in itself. You'd start it when you want the server running and<br /> it runs until an error or you close it.Navarr Barnierhttp://tech.gtaero.net/noreply@blogger.comtag:blogger.com,1999:blog-4350479895872803738.post-54770805151907027672012-04-28T13:02:00.000-07:002012-04-28T13:02:00.000-07:00Thanks, Navarr. So when would one start the script...Thanks, Navarr. So when would one start the script via the console - when launching the<br /> site? Would it then run, er, forever? I'm trying to get my head around the process here. Never done PHP via<br /> command line before (not even sure how on Mac...)Mityanoreply@blogger.comtag:blogger.com,1999:blog-4350479895872803738.post-6825502420687272842012-04-27T21:27:00.000-07:002012-04-27T21:27:00.000-07:00Hi there. I've been trying to get my head arou...Hi there. I've been trying to get my head around socket servers for days - as you say,<br /> the examples out there are either poorly explained or flatly don't work. For me, nothing worked; I kept<br /> getting socket bind errors, saying the host lookup failed. In fact that's what I get with yours, too, to<br /> start with, but then I changed the 'null' host reference to 'localhost'. This time the browser hung, as<br /> though it was loading a massive file. Is this expected behaviour? I opened a new tab and loaded the page<br /> again and it said the address was already in use. Presumably this means the previous page load started the<br /> server, and it's still running. How would I restart it? Do I need the console for this sort of thing? Thanks<br /> again.Mityanoreply@blogger.comtag:blogger.com,1999:blog-4350479895872803738.post-23856932760069903792012-04-27T21:59:00.000-07:002012-04-27T21:59:00.000-07:00If you try to run a server on a web page it will h...If you try to run a server on a web page it will hang, yes.<br><br>The<br /> expected use of this is over command line PHP, not as a web page. Binding as null means that it listens on<br /> all ports, if you're using shared hosting its possible that your provider doesn't allow it to listen for<br /> incoming connections. Using localhost means it'll listen to connections to it from within the server.<br>-----<br>Navarr<br /> T. Barnier<br>navarr@gtaero.net<br>http://navarr.me/Navarr Barnierhttp://tech.gtaero.net/noreply@blogger.comtag:blogger.com,1999:blog-4350479895872803738.post-82267091150418182752012-04-29T07:50:00.000-07:002012-04-29T07:50:00.000-07:00Presumably if it's, say, a chat ap, though, I&...Presumably if it's, say, a chat ap, though, I'd want it running constantly. So I'd<br /> start it when I launched the site and stop it... never?Mityanoreply@blogger.comtag:blogger.com,1999:blog-4350479895872803738.post-32821629050069641822012-04-29T16:01:00.000-07:002012-04-29T16:01:00.000-07:00Yes, essentially. Though in that case node us with...Yes, essentially. Though in that case node us with web sockets would be the better<br /> technology. If your host doesn't allow node, you probably won't be allowed to run a socket server.<br><br>If<br /> you're building a chat app you'll instead want to publish chats to a database and use Ajax polling and Ajax<br /> posting to keep it up to date.Navarr Barnierhttp://tech.gtaero.net/noreply@blogger.comtag:blogger.com,1999:blog-4350479895872803738.post-86961266199020666122013-03-05T21:21:00.000-08:002013-03-05T21:21:00.000-08:00No problem and glad it could help. :)No problem and glad it could help. :)Navarr Barnierhttp://tech.gtaero.net/noreply@blogger.comtag:blogger.com,1999:blog-4350479895872803738.post-44595705330917315122012-10-09T22:09:00.000-07:002012-10-09T22:09:00.000-07:00Meanwhile, thanks for the reply.I tried using putt...Meanwhile, thanks for the reply.<br><br><br>I tried using putty on<br /> port 35100 through SSH, but does not connect to the server either.Point out that the server is already using<br /> apache listen on port 35100, to address all requests that come to the script in question. La porta è aperta<br /> e il reindirizzamento funziona correttamente.<br>Maybe I did not understand what you mean by use<br /> PuTTY. Is there something I'm missing?<br><br><br>thanks.Salvatore Campanellanoreply@blogger.comtag:blogger.com,1999:blog-4350479895872803738.post-79941468976087703202012-10-11T00:38:00.000-07:002012-10-11T00:38:00.000-07:00This isn't a web page script. This is intended...This isn't a web page script. This is intended to be run from the command line with its<br /> own port.Navarr Barnierhttp://tech.gtaero.net/noreply@blogger.comtag:blogger.com,1999:blog-4350479895872803738.post-3210713484572467992012-07-03T14:31:00.000-07:002012-07-03T14:31:00.000-07:00Hey thanks for this fantastic class! Awesome piece...Hey thanks for this fantastic class! Awesome piece of work! Just having a similar<br /> problem to others below.. When disconnecting/ inputting or connecting I receive this message <br><br><br>"PHP<br /> Warning: Parameter 1 to handle_connect() expected to be a reference, value given in<br /> /var/www/test/SocketServer.class.php on line 203"<br><br><br>or <br><br><br>"PHP<br /> Warning: Parameter 1 to handle_input() expected to be a reference, value given in<br /> /var/www/test/SocketServer.class.php on line 203<br>"<br>etc<br><br><br>And<br /> the functions do not run. Hopefully you can tell me where I'm being stupid!Tom Coateshttp://twitter.com/CoatezyUKnoreply@blogger.comtag:blogger.com,1999:blog-4350479895872803738.post-7583737551037458762012-07-03T14:46:00.000-07:002012-07-03T14:46:00.000-07:00I fixed it in the example. On that line just put a...I fixed it in the example. On that line just put an ampersand (&) in front of<br /> $this and $client.Navarr Barnierhttp://tech.gtaero.net/noreply@blogger.comtag:blogger.com,1999:blog-4350479895872803738.post-72100482664653282302013-03-21T17:22:00.000-07:002013-03-21T17:22:00.000-07:00If you want to be able to reference other clients,...If you want to be able to reference other clients, you'll want to create a hashmap with<br /> ids as keys and references to the socket object as values.<br><br><br>On connect you'll<br /> want to do something like $clientMap[1] = &$client;<br><br>You should also remember to<br /> clean this array up on disconnect.<br><br><br>However you should still be able to do<br /> $clients[2]->socket (there is no reason that shouldn't work so long as $clients[2] is set and the<br /> socket is still connected).<br><br><br>Let me know if you have any more problems. I<br /> think I'm going to re-evaluate the class and PSR-2 it and write tests and everything too. (Off topic, more<br /> of a reminder for myself)Navarr Barnierhttp://tech.gtaero.net/noreply@blogger.comtag:blogger.com,1999:blog-4350479895872803738.post-8994687747852397492012-11-11T17:39:00.000-08:002012-11-11T17:39:00.000-08:00Thank you.It's nice code.Thank you.It's nice code.Oneplusnoreply@blogger.com