View Single Post
Author Message
Blowst
Senior Member
Join Date: Feb 2011
Location: Korea, Republic of
Old 06-22-2017 , 13:40   Communicating with socket.io using sourcemod and socket
Reply With Quote #1

Hello, I'm trying to connect to my socket.io server with a plugin using socket(3.0.1)

It works nice when I connected to it with web browser(socket.io client)

But it just close the connection throwing close code 1002 when I connected with my plugin.

Have you guys any idea or solution?

here's my node.js server DEBUG LOG
Code:
Thu, 22 Jun 2017 17:20:03 GMT socket.io:socket joined room H6lgVyeHYXKaNQ7cAAAF
Thu, 22 Jun 2017 17:20:03 GMT engine upgrading existing transport
Thu, 22 Jun 2017 17:20:03 GMT engine:socket might upgrade socket transport from "polling" to "websocket"
Thu, 22 Jun 2017 17:20:03 GMT engine:ws received "Hello"
Thu, 22 Jun 2017 17:20:03 GMT engine:ws closing
Thu, 22 Jun 2017 17:20:13 GMT engine:polling closing
Thu, 22 Jun 2017 17:20:13 GMT engine:polling transport not writable - buffering orderly close
Thu, 22 Jun 2017 17:20:13 GMT socket.io:client client close with reason ping timeout
Thu, 22 Jun 2017 17:20:13 GMT socket.io:socket closing socket - reason ping timeout
And here's my plugins code

PHP Code:
#include <sourcemod>
#include <socket>

char roomId[21];
char yeast[8];

public 
OnPluginStart() {
    new 
Handle:socket SocketCreate(SOCKET_TCPOnSocketError);
    
SocketConnect(socketOnSocketConnectedOnSocketReceiveOnSocketDisconnected"110.45.113.25"3000);
}

public 
OnSocketConnected(Handle:socketany:arg) {
    
// socket is connected, send the http request
    
PrintToServer("######Socket Connected!######");
    
decl String:requestStr[512];
    
    
PrintToServer("######Let's Try to CONNECT to websocket!######");
    
Format(requestStrsizeof(requestStr), "GET /%s HTTP/1.1\r\nHost: %s\r\n\r\n""getstamp""localhost:3000");
    
SocketSend(socketrequestStr);
    
    
PrintToServer("######Let's Try to HANDSHAKE with websocket!######");    
    
Format(requestStrsizeof(requestStr), "GET /%s%s HTTP/1.1\r\nHost: %s\r\n\r\n""socket.io/?clienttype=SRCDS&EIO=3&transport=polling&t="yeast"localhost:3000");
    
SocketSend(socketrequestStr);
}

public 
OnSocketReceive(Handle:socketString:receiveData[], const dataSizeany:hFile)
{
    
PrintToServer(receiveData);
    if(
StrContains(receiveData"Yeast:"false) != -1)
    {
        
Format(yeast8"%s"receiveData[StrContains(receiveData"Yeast:"true) + 6]);
    }
    else if(
StrContains(receiveData"HTTP/1.1 200 OK"true) != -1)
    {
        
/*
        HTTP/1.1 200 OK
        Content-Type: application/octet-stream
        Content-Length: 101
        Access-Control-Allow-Origin: *
        Set-Cookie: io=iCx7MnGxL1aqNU1gAAAJ; Path=/; HttpOnly
        Date: Mon, 24 Apr 2017 08:03:02 GMT
        Connection: keep-alive
        */
        
decl String:requestStr[512];        
        
PrintToServer("READY TO GO!");
        
        
Format(roomId21"%s"receiveData[StrContains(receiveData"Set-Cookie: io="true) + 15]);
        
// request handshake
        
Format(requestStrsizeof(requestStr), "GET /%s%s HTTP/1.1\r\nHost: %s\r\nConnection: Upgrade\r\nUpgrade: WebSocket\r\nOrigin: http://127.0.0.1:3000/\r\nSec-WebSocket-Version: 13\r\nSec-WebSocket-Key: %s\r\n\r\n""socket.io/?clienttype=SRCDS&EIO=3&transport=websocket&sid="roomId"localhost:3000""YW4gc3JjZHMgd3MgdGVzdA==");
        
SocketSend(socketrequestStr);
    }
    else if(
StrContains(receiveData"HTTP/1.1 101 Switching Protocols"true) == 0)
    {
        
/*
        HTTP/1.1 101 Switching Protocols
        Upgrade: websocket
        Connection: Upgrade
        Sec-WebSocket-Accept: MWRJjQKH/LfNpV4m0z+2rF8En+k=
        Sec-WebSocket-Protocol: chat
        */
        
        
char acceptKey[29];
        
Format(acceptKey29"%s"receiveData[StrContains(receiveData"Sec-WebSocket-Accept: "true) + 22]);
        
PrintToServer("ACCEPT-KEY: %s"acceptKey);

        
char sTemp[11]; // Text, Masked Hello
            
sTemp[0] = 0x81;
            
sTemp[1] = 0x85;
            
sTemp[2] = 0x37;
            
sTemp[3] = 0xfa;
            
sTemp[4] = 0x21;
            
sTemp[5] = 0x3d;
            
sTemp[6] = 0x7f;
            
sTemp[7] = 0x9f;
            
sTemp[8] = 0x4d;
            
sTemp[9] = 0x51;
            
sTemp[10] = 0x58;
        
SocketSend(socketsTemp);                  
    }
    else
    {
        
PrintToServer("You received a data:");
        
PrintToServer(receiveData);
    }

When I send Ping packet to socket.io server, it prints these.

Code:
Thu, 22 Jun 2017 17:22:35 GMT socket.io:socket joined room 2yNY-DX-uNWmtmftAAAG
Thu, 22 Jun 2017 17:22:35 GMT engine upgrading existing transport
Thu, 22 Jun 2017 17:22:35 GMT engine:socket might upgrade socket transport from "polling" to "websocket"
Thu, 22 Jun 2017 17:22:45 GMT engine:socket client did not complete upgrade - closing transport
Thu, 22 Jun 2017 17:22:45 GMT engine:ws closing
Thu, 22 Jun 2017 17:24:00 GMT engine:polling closing
Thu, 22 Jun 2017 17:24:00 GMT engine:polling transport not writable - buffering orderly close
Thu, 22 Jun 2017 17:24:00 GMT socket.io:client client close with reason ping timeout
Thu, 22 Jun 2017 17:24:00 GMT socket.io:socket closing socket - reason ping timeout
__________________
Sorry about my poor English


Last edited by Blowst; 06-29-2017 at 01:00.
Blowst is offline