├── .cproject ├── .gitignore ├── .project ├── .settings └── language.settings.xml ├── Debug_shared ├── WSS_shared ├── makefile ├── objects.mk ├── sources.mk └── src │ ├── WSS.d │ └── subdir.mk ├── Debug_static ├── WSS_static ├── makefile ├── objects.mk ├── sources.mk └── src │ ├── WSS.d │ └── subdir.mk ├── LICENSE.md ├── README.md ├── README.md~ ├── Release_shared ├── WSS_shared ├── makefile ├── objects.mk ├── sources.mk └── src │ ├── WSS.d │ └── subdir.mk ├── Release_static ├── WSS_static ├── makefile ├── objects.mk ├── sources.mk └── src │ ├── WSS.d │ └── subdir.mk ├── WSS_shared_x86_Linux ├── WSS_static_armv6 ├── WSS_static_osx ├── WSS_static_x86_Linux └── src └── WSS.cpp /.cproject: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 31 | 35 | 36 | 37 | 38 | 42 | 43 | 44 | 47 | 50 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 105 | 108 | 109 | 113 | 114 | 115 | 119 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 172 | 176 | 177 | 178 | 179 | 183 | 184 | 185 | 188 | 197 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | 219 | 220 | 221 | 222 | 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230 | 231 | 232 | 233 | 234 | 235 | 236 | 237 | 238 | 239 | 240 | 245 | 248 | 249 | 253 | 254 | 255 | 258 | 266 | 272 | 273 | 274 | 275 | 276 | 277 | 278 | 279 | 280 | 281 | 282 | 283 | 284 | 285 | 286 | 287 | 288 | 289 | 290 | 291 | 292 | 293 | 294 | 295 | 296 | 297 | 298 | 299 | 300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 308 | 309 | 310 | 311 | 312 | 313 | 314 | 315 | 316 | 317 | 318 | 319 | 320 | 321 | 322 | 323 | 324 | 325 | 326 | 327 | 328 | 329 | 330 | 331 | 332 | 333 | 334 | 335 | 336 | 337 | 338 | 339 | 340 | 341 | 342 | 343 | 344 | 345 | 346 | 347 | 348 | 349 | 350 | 351 | 352 | 353 | 354 | 355 | 356 | 357 | 358 | 359 | 360 | 361 | 362 | 363 | 364 | 365 | 366 | 367 | 368 | 369 | 370 | 371 | 372 | 373 | 374 | 375 | 376 | 377 | 378 | 379 | 380 | 381 | 382 | 383 | 384 | 385 | 386 | 387 | 388 | 389 | 390 | 391 | 392 | 393 | 394 | 395 | 396 | 397 | 398 | 399 | 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 419 | 420 | 421 | 422 | 423 | 424 | 425 | 426 | 427 | 428 | 429 | 430 | 431 | 432 | 433 | 434 | 435 | 436 | 437 | 438 | 439 | 440 | 441 | 442 | 443 | 444 | 445 | 446 | 447 | 448 | 449 | 450 | 451 | 452 | 453 | 454 | 455 | 456 | 457 | 458 | 459 | 460 | 461 | 462 | 463 | 464 | 465 | 466 | 467 | 468 | 469 | 470 | 471 | 472 | 473 | 474 | 475 | 476 | 477 | 478 | 479 | 480 | 481 | 482 | 483 | 484 | 485 | 486 | 487 | 488 | 489 | 490 | 491 | 492 | 493 | 494 | 495 | 496 | 497 | 498 | 499 | 500 | 501 | 502 | 503 | 504 | 505 | 506 | 507 | 508 | 509 | 510 | 511 | 512 | 513 | 514 | 515 | 516 | 517 | 518 | 519 | 520 | 521 | 522 | 523 | 524 | 525 | 526 | 527 | 528 | 529 | 530 | 531 | 532 | 533 | 534 | 535 | 536 | 537 | 538 | 539 | 540 | 541 | 542 | 543 | 544 | 545 | 546 | 547 | 548 | 549 | 550 | 551 | 552 | 553 | 554 | 555 | 556 | 557 | 558 | 559 | 560 | 561 | 562 | 563 | 564 | 565 | 566 | 567 | 568 | 569 | 570 | 571 | 572 | 573 | 574 | 575 | 576 | 577 | 578 | 579 | 580 | 581 | 582 | 583 | 584 | 585 | 586 | 587 | 588 | 589 | 590 | 591 | 592 | 593 | 594 | 595 | 596 | 597 | 598 | 599 | 600 | 601 | 602 | 603 | 604 | 605 | 606 | 607 | 608 | 609 | 610 | 611 | 612 | 613 | 614 | 615 | 616 | 617 | 618 | 619 | 620 | 621 | 622 | 623 | 624 | 625 | 626 | 627 | 628 | 629 | 630 | 631 | 632 | 633 | 634 | 635 | 636 | 637 | 638 | 639 | 640 | 641 | 642 | 643 | 644 | 645 | 646 | 647 | 648 | 649 | 650 | 651 | 652 | 653 | 654 | 655 | 656 | 657 | 658 | 659 | 660 | 661 | 662 | 663 | 664 | 665 | 666 | 667 | 668 | 669 | 670 | 671 | 672 | 673 | 674 | 675 | 676 | 677 | 678 | 679 | 680 | 681 | 682 | 683 | 684 | 685 | 686 | 687 | 688 | 689 | 690 | 691 | 692 | 693 | 694 | 695 | 696 | 697 | 698 | 699 | 700 | 701 | 702 | 703 | 704 | 705 | 706 | 707 | 708 | 709 | 710 | 711 | 712 | 713 | 714 | 715 | 716 | 717 | 718 | 719 | 720 | 721 | 722 | 723 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files 2 | *.slo 3 | *.lo 4 | *.o 5 | 6 | # Compiled Dynamic libraries 7 | *.so 8 | *.dylib 9 | 10 | # Compiled Static libraries 11 | *.lai 12 | *.la 13 | *.a 14 | -------------------------------------------------------------------------------- /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | WSS 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.cdt.managedbuilder.core.genmakebuilder 10 | clean,full,incremental, 11 | 12 | 13 | ?name? 14 | 15 | 16 | 17 | org.eclipse.cdt.make.core.append_environment 18 | true 19 | 20 | 21 | org.eclipse.cdt.make.core.autoBuildTarget 22 | all 23 | 24 | 25 | org.eclipse.cdt.make.core.buildArguments 26 | 27 | 28 | 29 | org.eclipse.cdt.make.core.buildCommand 30 | make 31 | 32 | 33 | org.eclipse.cdt.make.core.buildLocation 34 | ${workspace_loc:/WSS/Debug} 35 | 36 | 37 | org.eclipse.cdt.make.core.cleanBuildTarget 38 | clean 39 | 40 | 41 | org.eclipse.cdt.make.core.contents 42 | org.eclipse.cdt.make.core.activeConfigSettings 43 | 44 | 45 | org.eclipse.cdt.make.core.enableAutoBuild 46 | false 47 | 48 | 49 | org.eclipse.cdt.make.core.enableCleanBuild 50 | true 51 | 52 | 53 | org.eclipse.cdt.make.core.enableFullBuild 54 | true 55 | 56 | 57 | org.eclipse.cdt.make.core.fullBuildTarget 58 | all 59 | 60 | 61 | org.eclipse.cdt.make.core.stopOnError 62 | true 63 | 64 | 65 | org.eclipse.cdt.make.core.useDefaultBuildCmd 66 | true 67 | 68 | 69 | 70 | 71 | org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder 72 | full,incremental, 73 | 74 | 75 | 76 | 77 | 78 | org.eclipse.cdt.core.cnature 79 | org.eclipse.cdt.core.ccnature 80 | org.eclipse.cdt.managedbuilder.core.managedBuildNature 81 | org.eclipse.cdt.managedbuilder.core.ScannerConfigNature 82 | 83 | 84 | -------------------------------------------------------------------------------- /.settings/language.settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /Debug_shared/WSS_shared: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stylpen/WSS/9149706e5099d37cea006802abaca3c9cc24f694/Debug_shared/WSS_shared -------------------------------------------------------------------------------- /Debug_shared/makefile: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Automatically-generated file. Do not edit! 3 | ################################################################################ 4 | 5 | -include ../makefile.init 6 | 7 | RM := rm -rf 8 | 9 | # All of the sources participating in the build are defined here 10 | -include sources.mk 11 | -include subdir.mk 12 | -include src/subdir.mk 13 | -include objects.mk 14 | 15 | ifneq ($(MAKECMDGOALS),clean) 16 | ifneq ($(strip $(C++_DEPS)),) 17 | -include $(C++_DEPS) 18 | endif 19 | ifneq ($(strip $(C_DEPS)),) 20 | -include $(C_DEPS) 21 | endif 22 | ifneq ($(strip $(CC_DEPS)),) 23 | -include $(CC_DEPS) 24 | endif 25 | ifneq ($(strip $(CPP_DEPS)),) 26 | -include $(CPP_DEPS) 27 | endif 28 | ifneq ($(strip $(CXX_DEPS)),) 29 | -include $(CXX_DEPS) 30 | endif 31 | ifneq ($(strip $(C_UPPER_DEPS)),) 32 | -include $(C_UPPER_DEPS) 33 | endif 34 | endif 35 | 36 | -include ../makefile.defs 37 | 38 | # Add inputs and outputs from these tool invocations to the build variables 39 | 40 | # All Target 41 | all: WSS_shared 42 | 43 | # Tool invocations 44 | WSS_shared: $(OBJS) $(USER_OBJS) 45 | @echo 'Building target: $@' 46 | @echo 'Invoking: GCC C++ Linker' 47 | g++ -L/usr/local/lib -o"WSS_shared" $(OBJS) $(USER_OBJS) $(LIBS) 48 | @echo 'Finished building target: $@' 49 | @echo ' ' 50 | 51 | # Other Targets 52 | clean: 53 | -$(RM) $(OBJS)$(C++_DEPS)$(C_DEPS)$(CC_DEPS)$(CPP_DEPS)$(EXECUTABLES)$(CXX_DEPS)$(C_UPPER_DEPS) WSS_shared 54 | -@echo ' ' 55 | 56 | .PHONY: all clean dependents 57 | .SECONDARY: 58 | 59 | -include ../makefile.targets 60 | -------------------------------------------------------------------------------- /Debug_shared/objects.mk: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Automatically-generated file. Do not edit! 3 | ################################################################################ 4 | 5 | USER_OBJS := 6 | 7 | LIBS := -lpthread -lwebsocketpp -lboost_program_options -lboost_system -lboost_thread -lboost_date_time -lboost_regex 8 | 9 | -------------------------------------------------------------------------------- /Debug_shared/sources.mk: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Automatically-generated file. Do not edit! 3 | ################################################################################ 4 | 5 | O_SRCS := 6 | CPP_SRCS := 7 | C_UPPER_SRCS := 8 | C_SRCS := 9 | S_UPPER_SRCS := 10 | OBJ_SRCS := 11 | ASM_SRCS := 12 | CXX_SRCS := 13 | C++_SRCS := 14 | CC_SRCS := 15 | OBJS := 16 | C++_DEPS := 17 | C_DEPS := 18 | CC_DEPS := 19 | CPP_DEPS := 20 | EXECUTABLES := 21 | CXX_DEPS := 22 | C_UPPER_DEPS := 23 | 24 | # Every subdirectory with source files must be described here 25 | SUBDIRS := \ 26 | src \ 27 | 28 | -------------------------------------------------------------------------------- /Debug_shared/src/WSS.d: -------------------------------------------------------------------------------- 1 | src/WSS.d: ../src/WSS.cpp 2 | -------------------------------------------------------------------------------- /Debug_shared/src/subdir.mk: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Automatically-generated file. Do not edit! 3 | ################################################################################ 4 | 5 | # Add inputs and outputs from these tool invocations to the build variables 6 | CPP_SRCS += \ 7 | ../src/WSS.cpp 8 | 9 | OBJS += \ 10 | ./src/WSS.o 11 | 12 | CPP_DEPS += \ 13 | ./src/WSS.d 14 | 15 | 16 | # Each subdirectory must supply rules for building sources it contributes 17 | src/%.o: ../src/%.cpp 18 | @echo 'Building file: $<' 19 | @echo 'Invoking: GCC C++ Compiler' 20 | g++ -DDEBUG -I/usr/local/include -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -o"$@" "$<" 21 | @echo 'Finished building: $<' 22 | @echo ' ' 23 | 24 | 25 | -------------------------------------------------------------------------------- /Debug_static/WSS_static: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stylpen/WSS/9149706e5099d37cea006802abaca3c9cc24f694/Debug_static/WSS_static -------------------------------------------------------------------------------- /Debug_static/makefile: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Automatically-generated file. Do not edit! 3 | ################################################################################ 4 | 5 | -include ../makefile.init 6 | 7 | RM := rm -rf 8 | 9 | # All of the sources participating in the build are defined here 10 | -include sources.mk 11 | -include subdir.mk 12 | -include src/subdir.mk 13 | -include objects.mk 14 | 15 | ifneq ($(MAKECMDGOALS),clean) 16 | ifneq ($(strip $(C++_DEPS)),) 17 | -include $(C++_DEPS) 18 | endif 19 | ifneq ($(strip $(C_DEPS)),) 20 | -include $(C_DEPS) 21 | endif 22 | ifneq ($(strip $(CC_DEPS)),) 23 | -include $(CC_DEPS) 24 | endif 25 | ifneq ($(strip $(CPP_DEPS)),) 26 | -include $(CPP_DEPS) 27 | endif 28 | ifneq ($(strip $(CXX_DEPS)),) 29 | -include $(CXX_DEPS) 30 | endif 31 | ifneq ($(strip $(C_UPPER_DEPS)),) 32 | -include $(C_UPPER_DEPS) 33 | endif 34 | endif 35 | 36 | -include ../makefile.defs 37 | 38 | # Add inputs and outputs from these tool invocations to the build variables 39 | 40 | # All Target 41 | all: WSS_static 42 | 43 | # Tool invocations 44 | WSS_static: $(OBJS) $(USER_OBJS) 45 | @echo 'Building target: $@' 46 | @echo 'Invoking: GCC C++ Linker' 47 | g++ -L/usr/local/lib -o"WSS_static" $(OBJS) $(USER_OBJS) $(LIBS) 48 | @echo 'Finished building target: $@' 49 | @echo ' ' 50 | 51 | # Other Targets 52 | clean: 53 | -$(RM) $(OBJS)$(C++_DEPS)$(C_DEPS)$(CC_DEPS)$(CPP_DEPS)$(EXECUTABLES)$(CXX_DEPS)$(C_UPPER_DEPS) WSS_static 54 | -@echo ' ' 55 | 56 | .PHONY: all clean dependents 57 | .SECONDARY: 58 | 59 | -include ../makefile.targets 60 | -------------------------------------------------------------------------------- /Debug_static/objects.mk: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Automatically-generated file. Do not edit! 3 | ################################################################################ 4 | 5 | USER_OBJS := /usr/local/lib/libwebsocketpp.a /usr/lib/libboost_program_options.a /usr/lib/libboost_system.a /usr/lib/libboost_thread.a /usr/lib/libboost_date_time.a /usr/lib/libboost_regex.a 6 | 7 | LIBS := -lpthread 8 | 9 | -------------------------------------------------------------------------------- /Debug_static/sources.mk: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Automatically-generated file. Do not edit! 3 | ################################################################################ 4 | 5 | O_SRCS := 6 | CPP_SRCS := 7 | C_UPPER_SRCS := 8 | C_SRCS := 9 | S_UPPER_SRCS := 10 | OBJ_SRCS := 11 | ASM_SRCS := 12 | CXX_SRCS := 13 | C++_SRCS := 14 | CC_SRCS := 15 | OBJS := 16 | C++_DEPS := 17 | C_DEPS := 18 | CC_DEPS := 19 | CPP_DEPS := 20 | EXECUTABLES := 21 | CXX_DEPS := 22 | C_UPPER_DEPS := 23 | 24 | # Every subdirectory with source files must be described here 25 | SUBDIRS := \ 26 | src \ 27 | 28 | -------------------------------------------------------------------------------- /Debug_static/src/WSS.d: -------------------------------------------------------------------------------- 1 | src/WSS.d: ../src/WSS.cpp 2 | -------------------------------------------------------------------------------- /Debug_static/src/subdir.mk: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Automatically-generated file. Do not edit! 3 | ################################################################################ 4 | 5 | # Add inputs and outputs from these tool invocations to the build variables 6 | CPP_SRCS += \ 7 | ../src/WSS.cpp 8 | 9 | OBJS += \ 10 | ./src/WSS.o 11 | 12 | CPP_DEPS += \ 13 | ./src/WSS.d 14 | 15 | 16 | # Each subdirectory must supply rules for building sources it contributes 17 | src/%.o: ../src/%.cpp 18 | @echo 'Building file: $<' 19 | @echo 'Invoking: GCC C++ Compiler' 20 | g++ -DDEBUG -I/usr/local/include -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -o"$@" "$<" 21 | @echo 'Finished building: $<' 22 | @echo ' ' 23 | 24 | 25 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | Copyright (c) 2012, Stephan Wypler. All rights reserved. 2 | 3 | Redistribution and use in source and binary forms, with or without 4 | modification, are permitted provided that the following conditions are met: 5 | * Redistributions of source code must retain the above copyright 6 | notice, this list of conditions and the following disclaimer. 7 | * Redistributions in binary form must reproduce the above copyright 8 | notice, this list of conditions and the following disclaimer in the 9 | documentation and/or other materials provided with the distribution. 10 | * Neither the name of the WSS Project nor the 11 | names of its contributors may be used to endorse or promote products 12 | derived from this software without specific prior written permission. 13 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 14 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16 | ARE DISCLAIMED. IN NO EVENT SHALL STEPHAN WYPLER BE LIABLE FOR ANY 17 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 18 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 19 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 20 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 21 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 22 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | WSS 2 | === 3 | 4 | WebSocket to TCP Gateway 5 | 6 | WSS is a WebSocket to TCP Gateway that was originally created to connect mosquitto.js to an MQTT broker. 7 | It creates a WebSocket server and forwards any WebSocket traffic to a TCP Server. 8 | For every new WebSocket connection a TCP connection to the destination server is created and bound to the corresponding WebSocket connection. 9 | This results in a 1:1 mapping of WebSocket and TCP connections and ensures that the responses of the TCP server arrive at the original WebSocket client. 10 | 11 | The goal was initially to create a bridge that is content INsensitive and only transmits the traffic AS IT IS from the broker to websocket clients (and reverse). 12 | However, this approach is not ideal for MQTT as some JavaScript clients (including the reference implementation from Paho) can't handle multiple MQTT messages arriving at once (or fragmented messages arriving partly) in a WebSocket message. 13 | In the mqtt branch of WSS is a version that reads the MQTT header and determins the length of the message. It uses that information to send exactly ONE MQTT message to the WebSocket client so that the JS libraries won't have problems. 14 | 15 | The Websocket server uses version 0.2 of websocket++ (https://github.com/zaphoyd/websocketpp/tree/0.2.x) and the Boost C++ Libraries (http://www.boost.org/). 16 | 17 | It was developed using the eclipse IDE. 18 | The project configuration assumes that the boost and websocket++ header files and libraries are located in /usr/local/include and /usr/local/lib respectively. For boost headers and libs is searched in /usr/include and /usr/lib. If that doesn't match your setup just edit the eclipse project properties or build manually. 19 | 20 | Manual build: 21 | To create an executale that uses shared libraries run 22 | 23 | g++ -o WSS_shared -I/usr/local/include -L/usr/local/lib -lpthread -lboost_program_options -lboost_regex -lboost_thread -lboost_system -lwebsocketpp src/WSS.cpp -O3 -Wall 24 | 25 | Make sure that you have all symlinks you need from libwebsocketpp.so.* to libwebsocketpp.so and to libwebsocketpp.so.0 and that the LD_LIBRARY_PATH contains the shared libraries when you execute the program. 26 | To create a static linked executable run 27 | 28 | g++ -o WSS_static src/WSS.cpp -I/usr/local/include /usr/local/lib/libwebsocketpp.a /usr/lib/libboost_thread.a /usr/lib/libboost_regex.a /usr/lib/libboost_system.a /usr/lib/libboost_program_options.a -lpthread -O3 -Wall 29 | -------------------------------------------------------------------------------- /README.md~: -------------------------------------------------------------------------------- 1 | WSS 2 | === 3 | 4 | WebSocket to TCP Gateway 5 | 6 | WSS is a WebSocket to TCP Gateway that was originally created to connect mosquitto.js to an MQTT broker. 7 | It creates a WebSocket server and forwards any WebSocket traffic to a TCP Server. 8 | For every new WebSocket connection a TCP connection to the destination server is created and bound to the corresponding WebSocket connection. 9 | This results in a 1:1 mapping of WebSocket and TCP connections and ensures that the responses of the TCP server arrive at the original WebSocket client. 10 | 11 | The goal was initially to create a bridge that is content INsensitive and only routes the traffic from A to B. 12 | However, this approach is not ideal for MQTT as some JavaScript clients (including the reference implementation from Paho) can't handle multiple MQTT messages arriving at once in a WebSocket message (as they are sent in one TCP segemnt by the broker). 13 | In the mqtt branch of WSS is an (experimental (no error recovery capabilities yet)) version that reads the MQTT header and determins the length of the message. It uses that information to send exactly ONE mqtt message to the WebSocket client. 14 | 15 | It uses websocket++ (https://github.com/zaphoyd/websocketpp) and the Boost C++ Libraries (http://www.boost.org/). 16 | 17 | The Websocket Server was developed using the eclipse IDE. 18 | It assumes the boost and websocket++ header files and libraries are located in /urs/local/include and /usr/local/lib respectively. If that doesn't match your setup just edit the eclipse project properties. 19 | 20 | You can also build manually: 21 | To create an executale that uses shared libraries run 22 | 23 | g++ -o WSS_shared -I/usr/local/include -L/usr/local/lib -lpthread -lboost_program_options -lboost_regex -lboost_thread -lboost_system -lwebsocketpp src/WSS.cpp -O3 -Wall 24 | 25 | Make sure that you have all symlinks you need from libwebsocketpp.so.* to libwebsocketpp.so and to libwebsocketpp.so.0 and that the LD_LIBRARY_PATH contains the shared libraries when you execute the program. 26 | To create a static linked executable run 27 | 28 | g++ -o WSS_static src/WSS.cpp -I/usr/local/include /usr/local/lib/libwebsocketpp.a /usr/local/lib/libboost_thread.a /usr/local/lib/libboost_regex.a /usr/local/lib/libboost_system.a /usr/local/lib/libboost_program_options.a -lpthread -O3 -Wall 29 | 30 | -------------------------------------------------------------------------------- /Release_shared/WSS_shared: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stylpen/WSS/9149706e5099d37cea006802abaca3c9cc24f694/Release_shared/WSS_shared -------------------------------------------------------------------------------- /Release_shared/makefile: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Automatically-generated file. Do not edit! 3 | ################################################################################ 4 | 5 | -include ../makefile.init 6 | 7 | RM := rm -rf 8 | 9 | # All of the sources participating in the build are defined here 10 | -include sources.mk 11 | -include subdir.mk 12 | -include src/subdir.mk 13 | -include objects.mk 14 | 15 | ifneq ($(MAKECMDGOALS),clean) 16 | ifneq ($(strip $(C++_DEPS)),) 17 | -include $(C++_DEPS) 18 | endif 19 | ifneq ($(strip $(C_DEPS)),) 20 | -include $(C_DEPS) 21 | endif 22 | ifneq ($(strip $(CC_DEPS)),) 23 | -include $(CC_DEPS) 24 | endif 25 | ifneq ($(strip $(CPP_DEPS)),) 26 | -include $(CPP_DEPS) 27 | endif 28 | ifneq ($(strip $(CXX_DEPS)),) 29 | -include $(CXX_DEPS) 30 | endif 31 | ifneq ($(strip $(C_UPPER_DEPS)),) 32 | -include $(C_UPPER_DEPS) 33 | endif 34 | endif 35 | 36 | -include ../makefile.defs 37 | 38 | # Add inputs and outputs from these tool invocations to the build variables 39 | 40 | # All Target 41 | all: WSS_shared 42 | 43 | # Tool invocations 44 | WSS_shared: $(OBJS) $(USER_OBJS) 45 | @echo 'Building target: $@' 46 | @echo 'Invoking: GCC C++ Linker' 47 | g++ -L/usr/local/lib -o"WSS_shared" $(OBJS) $(USER_OBJS) $(LIBS) 48 | @echo 'Finished building target: $@' 49 | @echo ' ' 50 | 51 | # Other Targets 52 | clean: 53 | -$(RM) $(OBJS)$(C++_DEPS)$(C_DEPS)$(CC_DEPS)$(CPP_DEPS)$(EXECUTABLES)$(CXX_DEPS)$(C_UPPER_DEPS) WSS_shared 54 | -@echo ' ' 55 | 56 | .PHONY: all clean dependents 57 | .SECONDARY: 58 | 59 | -include ../makefile.targets 60 | -------------------------------------------------------------------------------- /Release_shared/objects.mk: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Automatically-generated file. Do not edit! 3 | ################################################################################ 4 | 5 | USER_OBJS := 6 | 7 | LIBS := -lwebsocketpp -lpthread -lboost_program_options -lboost_regex -lboost_thread -lboost_system 8 | 9 | -------------------------------------------------------------------------------- /Release_shared/sources.mk: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Automatically-generated file. Do not edit! 3 | ################################################################################ 4 | 5 | O_SRCS := 6 | CPP_SRCS := 7 | C_UPPER_SRCS := 8 | C_SRCS := 9 | S_UPPER_SRCS := 10 | OBJ_SRCS := 11 | ASM_SRCS := 12 | CXX_SRCS := 13 | C++_SRCS := 14 | CC_SRCS := 15 | OBJS := 16 | C++_DEPS := 17 | C_DEPS := 18 | CC_DEPS := 19 | CPP_DEPS := 20 | EXECUTABLES := 21 | CXX_DEPS := 22 | C_UPPER_DEPS := 23 | 24 | # Every subdirectory with source files must be described here 25 | SUBDIRS := \ 26 | src \ 27 | 28 | -------------------------------------------------------------------------------- /Release_shared/src/WSS.d: -------------------------------------------------------------------------------- 1 | src/WSS.d: ../src/WSS.cpp 2 | -------------------------------------------------------------------------------- /Release_shared/src/subdir.mk: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Automatically-generated file. Do not edit! 3 | ################################################################################ 4 | 5 | # Add inputs and outputs from these tool invocations to the build variables 6 | CPP_SRCS += \ 7 | ../src/WSS.cpp 8 | 9 | OBJS += \ 10 | ./src/WSS.o 11 | 12 | CPP_DEPS += \ 13 | ./src/WSS.d 14 | 15 | 16 | # Each subdirectory must supply rules for building sources it contributes 17 | src/%.o: ../src/%.cpp 18 | @echo 'Building file: $<' 19 | @echo 'Invoking: GCC C++ Compiler' 20 | g++ -I/usr/local/include -O3 -Wall -c -fmessage-length=0 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -o"$@" "$<" 21 | @echo 'Finished building: $<' 22 | @echo ' ' 23 | 24 | 25 | -------------------------------------------------------------------------------- /Release_static/WSS_static: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stylpen/WSS/9149706e5099d37cea006802abaca3c9cc24f694/Release_static/WSS_static -------------------------------------------------------------------------------- /Release_static/makefile: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Automatically-generated file. Do not edit! 3 | ################################################################################ 4 | 5 | -include ../makefile.init 6 | 7 | RM := rm -rf 8 | 9 | # All of the sources participating in the build are defined here 10 | -include sources.mk 11 | -include subdir.mk 12 | -include src/subdir.mk 13 | -include objects.mk 14 | 15 | ifneq ($(MAKECMDGOALS),clean) 16 | ifneq ($(strip $(C++_DEPS)),) 17 | -include $(C++_DEPS) 18 | endif 19 | ifneq ($(strip $(C_DEPS)),) 20 | -include $(C_DEPS) 21 | endif 22 | ifneq ($(strip $(CC_DEPS)),) 23 | -include $(CC_DEPS) 24 | endif 25 | ifneq ($(strip $(CPP_DEPS)),) 26 | -include $(CPP_DEPS) 27 | endif 28 | ifneq ($(strip $(CXX_DEPS)),) 29 | -include $(CXX_DEPS) 30 | endif 31 | ifneq ($(strip $(C_UPPER_DEPS)),) 32 | -include $(C_UPPER_DEPS) 33 | endif 34 | endif 35 | 36 | -include ../makefile.defs 37 | 38 | # Add inputs and outputs from these tool invocations to the build variables 39 | 40 | # All Target 41 | all: WSS_static 42 | 43 | # Tool invocations 44 | WSS_static: $(OBJS) $(USER_OBJS) 45 | @echo 'Building target: $@' 46 | @echo 'Invoking: GCC C++ Linker' 47 | g++ -o"WSS_static" $(OBJS) $(USER_OBJS) $(LIBS) 48 | @echo 'Finished building target: $@' 49 | @echo ' ' 50 | 51 | # Other Targets 52 | clean: 53 | -$(RM) $(OBJS)$(C++_DEPS)$(C_DEPS)$(CC_DEPS)$(CPP_DEPS)$(EXECUTABLES)$(CXX_DEPS)$(C_UPPER_DEPS) WSS_static 54 | -@echo ' ' 55 | 56 | .PHONY: all clean dependents 57 | .SECONDARY: 58 | 59 | -include ../makefile.targets 60 | -------------------------------------------------------------------------------- /Release_static/objects.mk: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Automatically-generated file. Do not edit! 3 | ################################################################################ 4 | 5 | USER_OBJS := /usr/local/lib/libwebsocketpp.a /usr/lib/libboost_thread.a /usr/lib/libboost_regex.a /usr/lib/libboost_system.a /usr/lib/libboost_program_options.a 6 | 7 | LIBS := -lpthread 8 | 9 | -------------------------------------------------------------------------------- /Release_static/sources.mk: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Automatically-generated file. Do not edit! 3 | ################################################################################ 4 | 5 | O_SRCS := 6 | CPP_SRCS := 7 | C_UPPER_SRCS := 8 | C_SRCS := 9 | S_UPPER_SRCS := 10 | OBJ_SRCS := 11 | ASM_SRCS := 12 | CXX_SRCS := 13 | C++_SRCS := 14 | CC_SRCS := 15 | OBJS := 16 | C++_DEPS := 17 | C_DEPS := 18 | CC_DEPS := 19 | CPP_DEPS := 20 | EXECUTABLES := 21 | CXX_DEPS := 22 | C_UPPER_DEPS := 23 | 24 | # Every subdirectory with source files must be described here 25 | SUBDIRS := \ 26 | src \ 27 | 28 | -------------------------------------------------------------------------------- /Release_static/src/WSS.d: -------------------------------------------------------------------------------- 1 | src/WSS.d: ../src/WSS.cpp 2 | -------------------------------------------------------------------------------- /Release_static/src/subdir.mk: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Automatically-generated file. Do not edit! 3 | ################################################################################ 4 | 5 | # Add inputs and outputs from these tool invocations to the build variables 6 | CPP_SRCS += \ 7 | ../src/WSS.cpp 8 | 9 | OBJS += \ 10 | ./src/WSS.o 11 | 12 | CPP_DEPS += \ 13 | ./src/WSS.d 14 | 15 | 16 | # Each subdirectory must supply rules for building sources it contributes 17 | src/%.o: ../src/%.cpp 18 | @echo 'Building file: $<' 19 | @echo 'Invoking: GCC C++ Compiler' 20 | g++ -I/usr/local/include -O3 -Wall -c -fmessage-length=0 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -o"$@" "$<" 21 | @echo 'Finished building: $<' 22 | @echo ' ' 23 | 24 | 25 | -------------------------------------------------------------------------------- /WSS_shared_x86_Linux: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stylpen/WSS/9149706e5099d37cea006802abaca3c9cc24f694/WSS_shared_x86_Linux -------------------------------------------------------------------------------- /WSS_static_armv6: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stylpen/WSS/9149706e5099d37cea006802abaca3c9cc24f694/WSS_static_armv6 -------------------------------------------------------------------------------- /WSS_static_osx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stylpen/WSS/9149706e5099d37cea006802abaca3c9cc24f694/WSS_static_osx -------------------------------------------------------------------------------- /WSS_static_x86_Linux: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stylpen/WSS/9149706e5099d37cea006802abaca3c9cc24f694/WSS_static_x86_Linux -------------------------------------------------------------------------------- /src/WSS.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012, Stephan Wypler. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions are met: 6 | * * Redistributions of source code must retain the above copyright 7 | * notice, this list of conditions and the following disclaimer. 8 | * * Redistributions in binary form must reproduce the above copyright 9 | * notice, this list of conditions and the following disclaimer in the 10 | * documentation and/or other materials provided with the distribution. 11 | * * Neither the name of the WSS Project nor the 12 | * names of its contributors may be used to endorse or promote products 13 | * derived from this software without specific prior written permission. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL STEPHAN WYPLER BE LIABLE FOR ANY 19 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | 35 | #define VERSION "VERSION 0.2" 36 | 37 | // The Connection created on construction a new TCP connection. 38 | // It forwards incoming TCP traffic to the websocket. 39 | // Its send() method sends stuff (from the websocket) to the TCP endpoint 40 | 41 | class Connection : public boost::enable_shared_from_this{ 42 | public: 43 | Connection(websocketpp::server::handler::connection_ptr con, boost::asio::io_service &io_service) : 44 | websocket_connection(con), socket(io_service), readBuffer(8192){ 45 | #ifdef DEBUG 46 | std::cout << "Constructor of Connection" << std::endl; 47 | #endif 48 | boost::asio::ip::tcp::resolver resolver(io_service); 49 | boost::asio::ip::tcp::resolver::query query(Connection::hostname, Connection::port); 50 | boost::asio::ip::tcp::resolver::iterator endpoint_iterator = resolver.resolve(query); 51 | boost::asio::ip::tcp::resolver::iterator end; 52 | boost::system::error_code error = boost::asio::error::host_not_found; 53 | while (error && endpoint_iterator != end) { 54 | socket.close(); 55 | socket.connect(*endpoint_iterator++, error); 56 | } 57 | if (error) { 58 | #ifdef DEBUG 59 | std::cerr << "connection error" << std::endl; 60 | #endif 61 | websocket_connection->close(websocketpp::close::status::NORMAL, "cant establish tcp connection"); 62 | } 63 | #ifdef DEBUG 64 | std::cout << "Created new Connection at " << this << std::endl; 65 | #endif 66 | } 67 | 68 | ~Connection(){ 69 | #ifdef DEBUG 70 | std::cout << "Begin of Destructor at " << this << std::endl; 71 | #endif 72 | websocket_connection->close(websocketpp::close::status::NORMAL, "closing connection"); 73 | #ifdef DEBUG 74 | std::cout << "End of Destructor" << std::endl; 75 | #endif 76 | } 77 | 78 | void receive(const boost::system::error_code& error, size_t bytes_transferred) { 79 | if (!error && websocket_connection->get_state() == websocketpp::session::state::OPEN) { 80 | std::string message(readBuffer.data(), bytes_transferred); 81 | #ifdef DEBUG 82 | std::cout << "received TCP segment from broker " << bytes_transferred << " bytes: "; 83 | unsigned int i = 0; 84 | for(std::vector::iterator it = readBuffer.begin(); it != readBuffer.end() && i <= message.size(); it++, i++){ 85 | std::cout << std::setw(2) << std::setfill('0') << std::hex << (short)*it << " "; 86 | } 87 | std::cout << std::dec << std::endl; 88 | std::cout << "plaintext: " << message << std::endl; 89 | #endif 90 | websocket_connection->send(message, websocketpp::frame::opcode::BINARY); 91 | socket.async_receive( 92 | boost::asio::buffer(readBuffer), 93 | boost::bind(&Connection::receive, shared_from_this(), boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); 94 | } 95 | else { 96 | #ifdef DEBUG 97 | std::cout << "I'm done (stopping async_receive)" << std::endl; 98 | #endif 99 | if(error != boost::asio::error::operation_aborted) 100 | websocket_connection->close(websocketpp::close::status::NORMAL, "failed to receive data from broker"); 101 | } 102 | } 103 | 104 | void send(const std::string &message) { 105 | #ifdef DEBUG 106 | std::cout << "sent TCP segment to broker " << message.size() << " bytes: " ; 107 | unsigned int i = 0; 108 | for(std::string::const_iterator it = message.begin(); it != message.end() && i <= message.size(); it++, i++) 109 | std::cout << std::setw(2) << std::setfill('0') << std::hex << (short)*it << " "; 110 | std::cout << std::dec << std::endl; 111 | std::cout << "plaintext: " << message << std::endl; 112 | #endif 113 | try{ 114 | socket.write_some(boost::asio::buffer(message.c_str(), message.size())); 115 | }catch(boost::system::system_error &e){ 116 | #ifdef DEBUG 117 | std::cerr << "Write Error in TCP connection to broker: " << e.what() << std::endl; 118 | #endif 119 | websocket_connection->close(websocketpp::close::status::NORMAL, "cant close tcp connection"); 120 | } 121 | } 122 | 123 | void start() { 124 | #ifdef DEBUG 125 | std::cout << "begin of start(). Connection at " << this << std::endl; 126 | #endif 127 | socket.async_receive( 128 | boost::asio::buffer(readBuffer), 129 | boost::bind(&Connection::receive, shared_from_this(), boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); 130 | #ifdef DEBUG 131 | std::cout << "started async TCP receive" << std::endl; 132 | #endif 133 | } 134 | 135 | void stop() { 136 | #ifdef DEBUG 137 | std::cout << "begin of stop(). Connection at " << this << std::endl; 138 | #endif 139 | socket.cancel(); 140 | socket.close(); 141 | #ifdef DEBUG 142 | std::cout << "stopped async TCP receive" << std::endl; 143 | #endif 144 | } 145 | 146 | static std::string hostname; 147 | static std::string port; 148 | 149 | private: 150 | websocketpp::server::handler::connection_ptr websocket_connection; 151 | boost::asio::ip::tcp::socket socket; 152 | std::vector readBuffer; 153 | }; 154 | 155 | std::string Connection::hostname; 156 | std::string Connection::port; 157 | 158 | // The WebSocket++ handler 159 | class ServerHandler: public websocketpp::server::handler { 160 | public: 161 | void on_open(connection_ptr con) { 162 | if (connections.find(con) == connections.end()) { 163 | #ifdef DEBUG 164 | std::cout << "new connection, create new handler to process this message" << std::endl; 165 | #endif 166 | connections[con] = boost::shared_ptr(new Connection(con, con->get_io_service())); 167 | connections[con]->start(); 168 | } 169 | #ifdef DEBUG 170 | else 171 | std::cerr << "did I just reuse a connection pointer???" << std::endl; 172 | #endif 173 | } 174 | 175 | void on_message(connection_ptr con, message_ptr msg) { 176 | if (connections.find(con) != connections.end()){ 177 | #ifdef DEBUG 178 | std::cout << "received from websocket: " << msg->get_payload() << std::endl; 179 | #endif 180 | connections[con]->send(msg->get_payload()); 181 | } 182 | #ifdef DEBUG 183 | else 184 | std::cerr << "error receiving websocket message" << std::endl; 185 | #endif 186 | } 187 | 188 | void on_close(connection_ptr con) { 189 | std::map >::iterator it = connections.find(con); 190 | if (it != connections.end()) { 191 | connections[con]->stop(); 192 | connections[con].reset(); 193 | connections.erase(it); 194 | #ifdef DEBUG 195 | std::cout << "closing connection, delete corresponding handler. handlers left:" << connections.size() << std::endl; 196 | #endif 197 | } 198 | #ifdef DEBUG 199 | else 200 | std::cerr << "can't remove connection on close" << std::endl; 201 | #endif 202 | } 203 | 204 | void on_fail(connection_ptr con) { 205 | std::map >::iterator it = connections.find(con); 206 | if (it != connections.end()) { 207 | connections[con]->stop(); 208 | connections[con].reset(); 209 | connections.erase(it); 210 | #ifdef DEBUG 211 | std::cout << "something failed. deleted handler. number of handlers left:" << connections.size() << std::endl; 212 | #endif 213 | } 214 | #ifdef DEBUG 215 | else 216 | std::cerr << "there is nothing to clean up after something failed in the connection handler." << std::endl; 217 | #endif 218 | } 219 | private: 220 | std::map > connections; 221 | }; 222 | 223 | int main(int argc, char* argv[]){ 224 | boost::program_options::options_description description("Available options"); 225 | description.add_options() 226 | ("help", "produce this help message") 227 | ("websocketPort", boost::program_options::value(), "specify the port where the websocket server should listen.\nDefault is 1337") 228 | ("brokerHost", boost::program_options::value(), "specify the host of the MQTT broker.\nDefault is localhost") 229 | ("brokerPort", boost::program_options::value(), "specify the port where the MQTT broker listens.\nDefault is 1883") 230 | ("version", "print version number and exit") 231 | ("verbose", "print websocket error messages"); 232 | 233 | boost::program_options::variables_map variables_map; 234 | try { 235 | boost::program_options::store(boost::program_options::parse_command_line(argc, argv, description), variables_map); 236 | boost::program_options::notify(variables_map); 237 | if (variables_map.find("help") != variables_map.end()) { 238 | std::cout << description << "\n"; 239 | return 1; 240 | } 241 | if (variables_map.find("version") != variables_map.end()) { 242 | std::cout << VERSION << "\n"; 243 | return 0; 244 | } 245 | 246 | unsigned short websocketPort = variables_map.find("websocketPort") != variables_map.end() ? variables_map["websocketPort"].as() : 1337; 247 | Connection::hostname = variables_map.find("brokerHost") != variables_map.end() ? variables_map["brokerHost"].as() : "localhost"; 248 | Connection::port = variables_map.find("brokerPort") != variables_map.end() ? variables_map["brokerPort"].as() : "1883"; 249 | 250 | websocketpp::server::handler::ptr serverHandler = websocketpp::server::handler::ptr(new ServerHandler()); 251 | websocketpp::server websocketServer(serverHandler); 252 | 253 | boost::asio::signal_set signals(websocketServer.get_io_service(), SIGINT, SIGTERM); 254 | signals.async_wait(boost::bind(&websocketpp::server::stop, boost::ref(websocketServer), true, websocketpp::close::status::NORMAL, "websocket server quit")); 255 | 256 | websocketServer.alog().unset_level(websocketpp::log::alevel::ALL); 257 | websocketServer.elog().unset_level(websocketpp::log::elevel::ALL); 258 | if (variables_map.find("verbose") != variables_map.end()) { 259 | websocketServer.elog().set_level(websocketpp::log::elevel::RERROR); 260 | websocketServer.elog().set_level(websocketpp::log::elevel::FATAL); 261 | } 262 | websocketServer.listen(boost::asio::ip::tcp::v4(), websocketPort, 1); 263 | } catch(boost::program_options::unknown_option & e){ 264 | std::cerr << e.what() << std::endl; 265 | std::cout << description << "\n"; 266 | return 1; 267 | } catch(boost::system::system_error &e){ 268 | std::cerr << "System Error: " << e.what() << std::endl; 269 | return 1; 270 | } catch (std::exception& e) { 271 | std::cerr << "Exception: " << e.what() << std::endl; 272 | return 1; 273 | } 274 | return 0; 275 | } 276 | --------------------------------------------------------------------------------