Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: How can i connect socket.io server?  (Read 2706 times)

0 Members and 1 Guest are viewing this topic.

dntjdals0513

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
How can i connect socket.io server?
« on: April 26, 2014, 06:57:19 pm »
i'm triying to connect to socket.io server, but it doesn't work.

server source :

var socketio = require('socket.io');
var port = 3303;
var io = socketio.listen(port);

io.sockets.on('connection',function(socket){
   console.log('hi');
   io.sockets.on('connection',function(data){
      console.log(data);
   });
});

client source :

#include <SFML/Graphics.hpp>
#include <SFML/Network.hpp>

int main()
{
   sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
   sf::CircleShape shape(100.f);

   sf::IpAddress ipAddress = sf::IpAddress::LocalHost;

   //TCP
   
   sf::TcpSocket socket;
   sf::Socket::Status status = socket.connect(ipAddress, 3303);

   if(status != sf::Socket::Done)
   {
      printf("connection error!\n");
   }
   else{

      printf("IP Address : %s\n", ipAddress.toString().c_str());

   }

   if (socket.send("connection", 11) != sf::Socket::Done)
   {
      // error...
   }


   //UDP
   /*
   sf::UdpSocket socket;

   if (socket.bind(3303))
   {
      printf("connection error!\n");
   }

   if (socket.send("hi", 3, ipAddress, 3303) != sf::Socket::Done)
   {
      printf("sending error!\n");
   }
   */

   shape.setFillColor(sf::Color::Green);

   while (window.isOpen())
   {
      sf::Event event;
      while (window.pollEvent(event))
      {
         if (event.type == sf::Event::Closed){
            socket.disconnect();
            window.close();
         }
      }

      window.clear();
      window.draw(shape);
      window.display();
   }

   return 0;
}

there's no error when running client, but the server doesn't respond.

How can I connect to Node.js socket.io server in SFML client?

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: How can i connect socket.io server?
« Reply #1 on: April 26, 2014, 07:04:32 pm »
I don't know anything about socket.io, but judging by the stuff I see on its home page it seems to expect HTTP, not just TCP.

Through some links on that home page I also stumbled across a C++ client for socket.io (https://github.com/ebshimizu/socket.io-clientpp/blob/master/src/socket_io_client.cpp) so maybe that will give you additional hints.  It seems to be using an HTTP POST request.
« Last Edit: April 26, 2014, 07:07:23 pm by Ixrec »