user4749265
user4749265

Reputation:

How to use WebSocket in XAMPP server

How to enable websocket in xampp server? I use windows 10. I want to build a chat based on websocket and but I can't connect to the websocket.

I found this article http://phppot.com/php/simple-php-chat-using-websocket/ but I don't understand how to connect php xampp to websocket.

Upvotes: 7

Views: 17929

Answers (4)

Jenner Palacios
Jenner Palacios

Reputation: 11

I made some changes and got it working because I already have a website running on XAMPP.

  1. "php-socket.php" → Change line 3:

define('PORT',"8090");

to:

define('PORT',"1337");

  1. "php-socket.php" → Change line 11:

socket_bind($socketResource, 0, PORT);

to:

socket_bind($socketResource, HOST_NAME, PORT);

  1. "index.php" → Change line 25:

var websocket = new WebSocket("ws://localhost:8090/demo/php-socket.php");

to:

var websocket = new WebSocket("ws://localhost:1337/demo/php-socket.php");

  1. Make a .bat file inside this demo folder:
@echo off
"C:\xampp\php\php.exe" -q "C:\xampp\htdocs\demo\php-socket.php"
  1. Launch the .bat file, and keep the cmd open as this is the socket listener or server; as soon as you close it, the frontend will say "Connection Closed"

Upvotes: 0

Daniel Richter
Daniel Richter

Reputation: 858

I tried get to run the example too, with the same error. Following solution is working:

download the example code and copy to your htdocs folder.

open two tabs in your Browser. 1. run the php-socket.php and let it load. 2. run the index.php in the 2nd tab and the example will work.

Upvotes: 1

Siim Kasepõld
Siim Kasepõld

Reputation: 69

Uncomment extension=php_sockets.dll in php.ini file to enable websockets in php.

Upvotes: 5

Wendell jean kuci
Wendell jean kuci

Reputation: 31

Simple.

Go to the project folder and run the "php php-socket.php" command on the terminal and the websocket server will works.

Make sure that the php path is in the environment variables, or run with full php path: "c:/xampp/full_path_to_php/php php-socket.php".

After running the server, access index.php with your browser and the chat will works.

php-socket.php is a script that creates a websocket service, class.chathandler.php is a chat implementation on the server side and index.php is the client side.

Please, sorry for my English.

Upvotes: 3

Related Questions