Jump to content
Sign in to follow this  
bilcan

Websocket Server Start Error

Recommended Posts

unit Unit1;

interface

uses
  NodeJS.Core,
  System.Types, System.Types.Convert, System.Objects, System.Device.Storage,
  System.Streams, System.Reader, System.Writer, System.JSON, System.IOUtils,
  SmartNJ.System, SmartNJ.Device.Storage, SmartNJ.Application,
  SmartNJ.Server.WebSocket;

type
  TNodeProgram = class(TObject)
  private
    FServer:    TNJWebSocketServer;
  protected
    procedure   HandleServerStarted(Sender: TObject);
  public
    procedure   Execute;
    constructor Create; virtual;
    destructor  Destroy; override;
  end;

implementation

constructor TNodeProgram.Create;
begin
  inherited Create;
  FServer := TNJWebSocketServer.Create;
  FServer.Port := 3500;
  FServer.OnAfterServerStarted := HandleServerStarted;
end;

destructor TNodeProgram.Destroy;
begin
  FServer.free;
  inherited;
end;

procedure TNodeProgram.Execute;
begin
  FServer.Start();
end;

procedure TNodeProgram.HandleServerStarted(Sender: TObject);
begin
  WritelnF('Server listening on port %d', [FServer.port]);
end;

end.

 

And I got following error message. (I installed nodejs websocket)

 

[info] Program starting

E:\SmartMobile\Server\output\Server.js:5076
         throw EW3Exception.CreateFmt($New(ENJWebSocketServer),"Failed to create websocket server object, system threw exception %s with message [%s]",[TObject.ClassName(e$11.ClassType), e$11.FMessage]);
         ^
[object Object]
[info] Program stopped

 

Share this post


Link to post
Share on other sites

Have you installed the websocket package in the output directory?
You write "I installed nodejs websocket" but it has to be the "ws" package and it has to be in the same folder as the project compiled file.

1. Open shell (CMD) and cd to the project folder
2. CD into the output folder (just compile to create it)
3. in the shell window, type "npm install ws". Npm (node package manager) will now download and place the ws (websocket) package files relative to the compiled *.js file (your compiled project file).
4. then run the project

Note: To build a dependency file, add the "--save" param to the NPM command, it will then create a packages file. This is cool when you want to push your code to amazon etc. because node will then notice this and install all the dependencies in one-go :)

I tested your code and it worked on the first try after i installed the ws library


 

websocket.png

Share this post


Link to post
Share on other sites

Its actually one of the coolest things about node.js!

Node also has clang (c++ compiler) built-in, so if you use native packages it will actually re-compile node.js on the spot (!)
But the majority of modules are raw JS and can be safely copied between systems.

So you can write a server on windows, and just copy it to linux without any change :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×