Skip to content

Commit

Permalink
Added functions for connecting to endpoints
Browse files Browse the repository at this point in the history
Example updated
  • Loading branch information
ebshimizu committed Dec 20, 2012
1 parent 1dea8ab commit ece36ce
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 4 deletions.
14 changes: 12 additions & 2 deletions msvc/sample/sample/main.cpp
Expand Up @@ -3,7 +3,9 @@
*
* Demonstrates basic usage of the socket.io client.
* Sets up a connection, registers an event handler, and sends an event.
* The demo socket.io server emits an "example" event in response.
* Uses the test.js server provided with this example code.
* Note that the events the server sends back are not bound to anything, but are logged
* in the output of the application with full logging enabled.
*/

#include "stdafx.h"
Expand All @@ -13,7 +15,7 @@ using namespace socketio;

int main(int /*argc*/, char* /*argv*/ []) {
// websocket++ expects urls to begin with "ws://" not "http://"
std::string uri = "ws://localhost:8082/";
std::string uri = "ws://localhost:8080/";

try {
// Create and link handler to websocket++ callbacks.
Expand Down Expand Up @@ -44,6 +46,14 @@ int main(int /*argc*/, char* /*argv*/ []) {

handler->bind_event("example", &socketio_events::example);

// After connecting, send a connect message if using an endpoint
handler->connect_endpoint("/chat");

std::getchar();

// Then to connect to another endpoint with the same socket, we call connect again.
handler->connect_endpoint("/news");

std::getchar();

handler->emit("test", "hello!");
Expand Down
4 changes: 2 additions & 2 deletions msvc/sample/sample/sample.vcxproj
Expand Up @@ -104,12 +104,12 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
<IncludePath>..\..\..\src;$(BOOSTROOT);..\..\..\lib\websocketpp\src;..\..\..\lib\rapidjson\include;$(IncludePath)</IncludePath>
<LibraryPath>$(BOOSTROOT)\bin.v2\libs\;$(LibraryPath)</LibraryPath>
<LibraryPath>$(BOOSTROOT)\stage\lib;$(LibraryPath)</LibraryPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
<IncludePath>..\..\..\src;$(BOOSTROOT);..\..\..\lib\websocketpp\src;..\..\..\lib\rapidjson\include;$(IncludePath)</IncludePath>
<LibraryPath>C:\Program Files\boost\bin.v2\libs\;$(LibraryPath)</LibraryPath>
<LibraryPath>$(BOOSTROOT)\stage\lib;$(LibraryPath)</LibraryPath>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
Expand Down
26 changes: 26 additions & 0 deletions msvc/sample/test.js
@@ -0,0 +1,26 @@
var io = require('socket.io').listen(8080);

var chat = io
.of('/chat')
.on('connection', function (socket) {
socket.emit('a message', {
that: 'only'
, '/chat': 'will get'
});
chat.emit('a message', {
everyone: 'in'
, '/chat': 'will get'
});
});

var news = io
.of('/news')
.on('connection', function (socket) {
socket.emit('item', { news: 'item' });
});

io.sockets.on('connection', function (socket) {
socket.on('test', function (msg) {
console.log('Test message: ', msg);
});
});
10 changes: 10 additions & 0 deletions src/socket_io_client.cpp
Expand Up @@ -216,6 +216,16 @@ void socketio_client_handler::send(unsigned int type, std::string endpoint, std:
send(package.str());
}

void socketio_client_handler::connect_endpoint(std::string endpoint)
{
send("1::" + endpoint);
}

void socketio_client_handler::disconnect_endpoint(std::string endpoint)
{
send("0::" + endpoint);
}

void socketio_client_handler::emit(std::string name, Document& args, std::string endpoint, unsigned int id)
{
// Add the name to the data being sent.
Expand Down
6 changes: 6 additions & 0 deletions src/socket_io_client.hpp
Expand Up @@ -86,6 +86,12 @@ class socketio_client_handler : public client::handler {
// Allows user to send a custom socket.IO message
void send(unsigned int type, std::string endpoint, std::string msg, unsigned int id = 0);

// Signal connection to the desired endpoint. Allows the use of the endpoint once message is successfully sent.
void connect_endpoint(std::string endpoint);

// Signal disconnect from specified endpoint.
void disconnect_endpoint(std::string endpoint);

// Emulates the emit function from socketIO (type 5)
void emit(std::string name, Document& args, std::string endpoint = "", unsigned int id = 0);
void emit(std::string name, std::string arg0, std::string endpoint = "", unsigned int id = 0);
Expand Down

0 comments on commit ece36ce

Please sign in to comment.