Skip to content
This repository has been archived by the owner on Aug 25, 2022. It is now read-only.

Adds the possibility to set the stream context of the connection #103

Closed
wants to merge 4 commits into from

Conversation

vincentdieltiens
Copy link
Contributor

Here is a example of how to set the context with custom headers :

<?php

require_once 'vendor/autoload.php';

use ElephantIO\Client;
use ElephantIO\Engine\SocketIO\Version1X;

$url = 'http://localhost:12345';

$httpContext = [
    'header' => [
        'Origin: http://my-domain.com'
    ]
];
$client = new Client(
    new Version1X($url, [ 
        'context' => [
            'http' => $httpContext
        ]
    ])
);
$client->initialize();
$client->emit('message', [ 'message' => 'Hello world']);
$client->close();

@vincentdieltiens
Copy link
Contributor Author

I think now that the PR is good :)

@Taluu
Copy link
Contributor

Taluu commented May 12, 2015

I'm not a fan of having both the context in a context property AND nested in the options property though (also as having a getOrigin method... what about the other type of headers ? I know I said that implementing those is not the goal of the library - it still isn't - but I guess you get my drift :))

though I'm not against a getContext method... which would format everything as it needs to be (adding the timeout, the origin, ... etc)

@vincentdieltiens
Copy link
Contributor Author

(also as having a getOrigin method... what about the other type of headers ? I know I said that implementing those is not the goal of the library - it still isn't - but I guess you get my drift :))

I don't know if it's necessary to set the same headers for the upgradeTransport request as for the socket. Is it ?
If it's the case, well, the getOrigin is not the best way to do this.

I'm not a fan of having both the context in a context property AND nested in the options property though
...

As we need the context both in the connect() and handshake(), I have to store the context in a var...
Maybe you don't just want to be a class property ?
The option property is just used to let the developer give the stream context.

though I'm not against a getContext method... which would format everything as it needs to be (adding the timeout, the origin, ... etc)

Mmmh, you decide that the input should be compatible with php stream context :-P
So there is no need to format anything...
If we have to format the input... I prefer using a Context class...

@FnTm
Copy link

FnTm commented Aug 11, 2015

This is something that I would very like to see in the main branch.

@Taluu
Copy link
Contributor

Taluu commented Aug 11, 2015

On a second thought, having another property may do the trick, but I still think it is awkward to have the context in the options AND on its own. It should be one or the other. A reference could be used though, called here and there...

I'll try to make something up, if I ever get the time :{

Taluu added a commit that referenced this pull request Sep 12, 2015
Better context handling (Part merg from #103, fixes #81)
@Taluu
Copy link
Contributor

Taluu commented Sep 12, 2015

Merged manually, with just a little bit of delay (sorry about that !)

@Taluu Taluu closed this Sep 12, 2015
@lainard
Copy link

lainard commented Jul 25, 2016

it does not work

@Taluu
Copy link
Contributor

Taluu commented Jul 25, 2016

Could you be more specific ? Could you open an issue with details ? Thanks.

@lainard
Copy link

lainard commented Jul 25, 2016

Hi Taluu, basicly i am trying to emit new_message which is node on https mode

<?php

require_once 'vendor/autoload.php';

use ElephantIO\Client;
use ElephantIO\Engine\SocketIO\Version1X;

$url = 'https://185.83.138.133:3000';

$httpContext = [
    'header' => [
        'Origin: http://my-domain.com'
    ]
];
$client = new Client(
    new Version1X($url, [ 
        'context' => [
            'http' => $httpContext
        ]
    ])
);
$client->initialize();
$client->emit('new_message', [ 'name' => 'my name']);
$client->close();

and i get error that the

PHP Fatal error: Uncaught exception 'ElephantIO\Exception\ServerConnectionFailureException' with message 'An error occurred while trying to establish a connection to the server' in /root/vendor/wisembly/elephant.io/src/Engine/SocketIO/Version1X.php:166
Stack trace:
#0 /root/vendor/wisembly/elephant.io/src/Engine/SocketIO/Version1X.php(48): ElephantIO\Engine\SocketIO\Version1X->handshake()
#1 /root/vendor/wisembly/elephant.io/src/Client.php(60): ElephantIO\Engine\SocketIO\Version1X->connect()
#2 /root/elephan.io(22): ElephantIO\Client->initialize()
#3 {main}
thrown in /root/vendor/wisembly/elephant.io/src/Engine/SocketIO/Version1X.php on line 166

while when i use chrome socket.io tester it works perfectly

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants