Coder Social home page Coder Social logo

phpwebsocket's Introduction

PHPWEBSOCKET

So here is a quick hack to implement websockets in php.
As of Feb/10 the only browsers that support websockets are Google Chrome and Safari nightlies

Changelog

  • 2010.02.18 - Chatbot demo extending websocket class
  • 2010.02.18 - Websocket class for better reuse
  • 2010.02.15 - Added Users list to keep track of handshakes
  • 2010.02.15 - Minor cosmetic changes

Client side

var host = "ws://localhost:12345/websocket/server.php";
try{
  socket = new WebSocket(host);
  log('WebSocket - status '+socket.readyState);
  socket.onopen    = function(msg){ log("Welcome - status "+this.readyState); };
  socket.onmessage = function(msg){ log("Received: "+msg.data); };
  socket.onclose   = function(msg){ log("Disconnected - status "+this.readyState); };
}
catch(ex){ log(ex); }

View source code of client.html

Server side

log("Handshaking...");
list($resource,$host,$origin) = getheaders($buffer);
$upgrade = "HTTP/1.1 101 Web Socket Protocol Handshake\r\n" .
		   "Upgrade: WebSocket\r\n" .
		   "Connection: Upgrade\r\n" .
		   "WebSocket-Origin: " . $origin . "\r\n" .
		   "WebSocket-Location: ws://" . $host . $resource . "\r\n" .
		   "\r\n";
$handshake = true;
socket_write($socket,$upgrade.chr(0),strlen($upgrade.chr(0)));

View source code of server.php

Steps to run the test:

  • Save both files, client.html and server.php, in a folder in your local server running Apache and PHP.
  • From the command line, run the server.php program to listen for socket connections.
  • Open Google Chrome and point to the client.html page
  • Done, your browser now has a full-duplex channel with the server.
  • Start sending commands to the server to get some responses.

WebSockets for the masses!

Author

George Nava

http://georgenava.appspot.com
http://mylittlehacks.appspot.com
http://twitter.com/georgenava

phpwebsocket's People

Contributors

georgenava avatar

Watchers

James Cloos avatar osmen avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.