Skip to content

Commit 176e17f

Browse files
Updated all examples to remove protocol, clean up, and test
1 parent 6eee057 commit 176e17f

File tree

12 files changed

+2780
-748
lines changed

12 files changed

+2780
-748
lines changed

example_client_blob/src/ofApp.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
//--------------------------------------------------------------
44
void ofApp::setup(){
5+
// connect to either the "server blob" or "server blob video" example!
56
client.connect("localhost", 9093);
67

78
client.addListener(this);
@@ -57,11 +58,14 @@ void ofApp::onIdle( ofxLibwebsockets::Event& args ){
5758
void ofApp::onMessage( ofxLibwebsockets::Event& args ){
5859
if ( locked ) return;
5960
// need to load this next frame!
60-
//cout << args.isBinary << ":" << args.size << endl;
61-
buff.clear();
62-
buff.set(args.data.getBinaryBuffer(), args.data.size());
63-
locked = true;
64-
needToLoad = true;
61+
if ( args.isBinary ){
62+
buff.clear();
63+
buff.set(args.data.getBinaryBuffer(), args.data.size());
64+
locked = true;
65+
needToLoad = true;
66+
} else {
67+
// got a string message
68+
}
6569
}
6670

6771
//--------------------------------------------------------------

example_client_hello_world/example_client_hello_world.xcodeproj/project.pbxproj

Lines changed: 2704 additions & 706 deletions
Large diffs are not rendered by default.

example_client_hello_world/src/ofApp.cpp

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,33 @@
22

33
//--------------------------------------------------------------
44
void ofApp::setup(){
5+
ofBackground(0);
56
ofSetLogLevel(OF_LOG_VERBOSE);
6-
client.connect("echo.websocket.org");
7-
// client.connect("echo.websocket.org", true); // optionally use SSL
7+
// basic connection:
8+
// client.connect("echo.websocket.org");
9+
// OR optionally use SSL
10+
// client.connect("echo.websocket.org", true);
11+
12+
// advanced: set keep-alive timeouts for events like
13+
// loss of internet
14+
15+
// 1 - get default options
16+
ofxLibwebsockets::ClientOptions options = ofxLibwebsockets::defaultClientOptions();
17+
18+
// 2 - set basic params
19+
options.host = "echo.websocket.org";
20+
21+
// 3 - set keep alive params
22+
// BIG GOTCHA: on BSD systems, e.g. Mac OS X, these time params are system-wide
23+
// ...so ka_time just says "check if alive when you want" instead of "check if
24+
// alive after X seconds"
25+
options.ka_time = 1;
26+
// options.ka_probes = 1;
27+
// options.ka_interval = 1;
28+
29+
// 4 - connect
30+
client.connect(options);
31+
832
ofSetLogLevel(OF_LOG_ERROR);
933

1034
client.addListener(this);
@@ -17,6 +41,8 @@ void ofApp::update(){
1741

1842
//--------------------------------------------------------------
1943
void ofApp::draw(){
44+
ofDrawBitmapString("Type anywhere to send 'hello' to your server\nCheck the console for output!", 10,20);
45+
ofDrawBitmapString(client.isConnected() ? "Client is connected" : "Client disconnected :(", 10,50);
2046
}
2147

2248
//--------------------------------------------------------------

example_client_sharedcanvas/src/ofApp.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@
22

33
//--------------------------------------------------------------
44
void ofApp::setup(){
5-
6-
ofxLibwebsockets::ClientOptions options = ofxLibwebsockets::defaultClientOptions();
7-
options.port = 9092;
8-
options.protocol = "of-protocol";
9-
client.connect( options );
5+
client.connect( "localhost", 9092 );
106
bConnected = false;
117

128
client.addListener(this);

example_particles_client/src/ofApp.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ ofPixels sendPixels;
44

55
//--------------------------------------------------------------
66
void ofApp::setup(){
7-
ofSetVerticalSync(true);
7+
ofSetFrameRate(60);
88

99
// setup client @ port 9093
1010
client.connect("localhost", 9093);

example_server_binary_video/bin/data/web/js/main.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,10 @@ function onMessage( messageEvent ){
125125
function setupSocket(){
126126
// setup!
127127
if (BrowserDetect.browser == "Firefox") {
128-
socket = new MozWebSocket( get_appropriate_ws_url(), "of-protocol" );
128+
socket = new MozWebSocket( get_appropriate_ws_url() );
129129
socket.binaryType = "arraybuffer";
130130
} else {
131-
socket = new WebSocket( get_appropriate_ws_url(), "of-protocol");
131+
socket = new WebSocket( get_appropriate_ws_url());
132132
socket.binaryType = "arraybuffer";
133133
}
134134

example_server_binary_video/src/ofApp.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ void ofApp::setup(){
1717

1818
ofxLibwebsockets::ServerOptions options = ofxLibwebsockets::defaultServerOptions();
1919
options.port = 9093;
20-
options.protocol = "of-protocol";
2120
bool connected = server.setup( options );
2221

2322
// this adds your app as a listener for the server

example_server_blob/bin/data/web/js/main.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,10 @@ function onMessage( messageEvent ){
6969
function setupSocket(){
7070
// setup!
7171
if (BrowserDetect.browser == "Firefox") {
72-
socket = new MozWebSocket( get_appropriate_ws_url(), "of-protocol" );
72+
socket = new MozWebSocket( get_appropriate_ws_url());
7373
socket.binaryType = "blob";
7474
} else {
75-
socket = new WebSocket( get_appropriate_ws_url(), "of-protocol");
75+
socket = new WebSocket( get_appropriate_ws_url());
7676
socket.binaryType = "blob";
7777
}
7878

example_server_blob/src/ofApp.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ void ofApp::setup(){
2121

2222
ofxLibwebsockets::ServerOptions options = ofxLibwebsockets::defaultServerOptions();
2323
options.port = 9093;
24-
options.protocol = "of-protocol";
25-
options.bBinaryProtocol = true;
2624

2725
bool connected = server.setup( options );
2826

@@ -219,7 +217,14 @@ void ofApp::mouseMoved(int x, int y ){}
219217
void ofApp::mouseDragged(int x, int y, int button){}
220218

221219
//----------------------------------------------------------- ---
222-
void ofApp::mousePressed(int x, int y, int button){}
220+
void ofApp::mousePressed(int x, int y, int button){
221+
string url = "http";
222+
if ( server.usingSSL() ){
223+
url += "s";
224+
}
225+
url += "://localhost:" + ofToString( server.getPort() );
226+
ofLaunchBrowser(url);
227+
}
223228

224229
//--------------------------------------------------------------
225230
void ofApp::mouseReleased(int x, int y, int button){}

example_server_blobvideo/src/ofApp.cpp

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
11
#include "ofApp.h"
22

3+
// quality of image: higher is more readable, lower is faster!
4+
int jpegquality = 100;
5+
36
//--------------------------------------------------------------
47
void ofApp::setup(){
5-
// setup a server with default options on port 9092
6-
// - pass in true after port to set up with SSL
7-
//bool connected = server.setup( 9092 );
8-
9-
// Uncomment this to set up a server with a protocol
10-
// Right now, clients created via libwebsockets that are connecting to servers
11-
// made via libwebsockets seem to want a protocol. Hopefully this gets fixed,
12-
// but until now you have to do something like this:
13-
148
//setup video grabber
159
video.listDevices();
1610
bVideoSetup = video.initGrabber(640,480);
1711

12+
// setup a server with default options on port 9093
1813
ofxLibwebsockets::ServerOptions options = ofxLibwebsockets::defaultServerOptions();
1914
options.port = 9093;
2015
bool connected = server.setup( options );
@@ -42,7 +37,7 @@ void ofApp::update(){
4237
// the second param == quality. play with this to get a better framerate
4338
// you can also try resizing your image!
4439
//currentImage.resize(160, 120);
45-
unsigned char * compressed = turbo.compress(currentImage,50,&size);
40+
unsigned char * compressed = turbo.compress(currentImage,jpegquality,&size);
4641
server.sendBinary(compressed, size);
4742
free(compressed);
4843
}
@@ -59,6 +54,12 @@ void ofApp::draw(){
5954
}
6055
if ( bVideoSetup ) video.draw(0,0);
6156
ofDrawBitmapString(ofToString(ofGetFrameRate())+"fps", 10, 15);
57+
58+
ofSetColor(255);
59+
ofDrawBitmapString("WebSocket server setup at "+ofToString( server.getPort() ) + ( server.usingSSL() ? " with SSL" : " without SSL"), 10, 40);
60+
61+
ofDrawBitmapString("Click anywhere to open up browser client", 10, 60);
62+
ofDrawBitmapString("+/- changes image quality: "+ofToString(jpegquality), 10, 80);
6263
}
6364

6465
//--------------------------------------------------------------
@@ -107,7 +108,13 @@ void ofApp::onBroadcast( ofxLibwebsockets::Event& args ){
107108

108109
//--------------------------------------------------------------
109110
void ofApp::keyPressed(int key){
110-
111+
if ( key == '+'){
112+
jpegquality ++;
113+
jpegquality = jpegquality > 100 ? 100 : jpegquality;
114+
} else if (key == '-'){
115+
jpegquality--;
116+
jpegquality = jpegquality < 0 ? 0 : jpegquality;
117+
}
111118
}
112119

113120
//--------------------------------------------------------------
@@ -127,7 +134,12 @@ void ofApp::mouseDragged(int x, int y, int button){
127134

128135
//--------------------------------------------------------------
129136
void ofApp::mousePressed(int x, int y, int button){
130-
137+
string url = "http";
138+
if ( server.usingSSL() ){
139+
url += "s";
140+
}
141+
url += "://localhost:" + ofToString( server.getPort() );
142+
ofLaunchBrowser(url);
131143
}
132144

133145
//--------------------------------------------------------------

0 commit comments

Comments
 (0)