php websocket forwarding to nodejs

2019-08-16 23:20发布

im tring to create an adapter in php to forward messages from javascript client to nodejs server and vice versa.

WebSockets:

nodejs server: ws

php server: RatchetPHP

php client: RatchetPawl

Code:

websocket server in php: chatWSServer.php

connection class: connection.php

javascript client:

connection = new WebSocket('ws://localhost:9000');
connection.onmessage = (e) => {
    console.log(e.data);
}
connection.onopen = (e) => {
    console.log("connection established!");
}
connection.addEventListener('message', (resData) => {
    console.log("got message:", resData);
});

Problem:

i try to open a connection to the nodejs websocket server (localhost:8889), on javscript client connection to php websocket server (localhost:9000): chatWSServer.php - createConnection() and its working, but when the javascript client try to send a message i get:

Failed to execute 'send' on 'WebSocket': Still in CONNECTING state.

i think its because ratchet pawl connector loop is running - when i comment: loop->run() the client is able to send messages.

1条回答
走好不送
2楼-- · 2019-08-16 23:46

with the help from @WyriHaximus i managed to solve this.

ive created a ratchetpawl websocket client with the same loop as ratchet websocket server's loop. and now its non blocking!

see my repository for code solution.

查看更多
登录 后发表回答