mobileFX WebKitX CEF3 ActiveX 4.x
WebKitXCEF3Lib ActiveX Control / WebKitXCEF3 Object / StartNodeJS Method

Absolute path of the JavaScript program to execute.

In This Topic
    StartNodeJS Method
    In This Topic
    Description

    Runs a JavaScript program with node.js executive.

    Syntax
    Visual Basic
    Public Sub StartNodeJS( _
       ByVal JavascriptProgramPath As String _
    ) 
    Parameters
    JavascriptProgramPath

    Absolute path of the JavaScript program to execute.

    Remarks

    WebKitX can spawn a child node.js process and execute a JavaScript program in its context, by taking advantage of every NPM package available.

    To define the node.js path you must handle the OnCreate event and set Settings.nodejs_executableSettings.nodejs_modules_path and Settings.nodejs_command_line_arguments properties.

    You can set node.js to auto start when WebKitX starts by setting Settings.nodejs_auto_start property.

    You can enable node.js debugger and control node.js debugger from WebKitX by setting Settings.nodejs_debug_port.

    Example
    Private Sub WebKitX_OnCreate(ByVal Settings As WebKitXCEF3Lib.ISettings, CommandLineSwitches As String)
        
        On Error Resume Next
        
        With Settings
            
            .app_path = App.Path
            .accept_language_list = "es-CL,en-GB"
            .remote_debugging_port = 9999
            .persist_session_cookies = 0
            .persist_user_preferences = 0
            
            .nodejs_auto_start = False
            .nodejs_debug_port = 9998
            .nodejs_debugger_protocol = "inspector"
            .nodejs_executable = App.Path + "\etc\config\server\node.exe"
            .nodejs_modules_path = App.Path + "\etc\config\server\node_modules"
            .nodejs_command_line_arguments = " --stack-trace-limit=25 --interactive --report-compact --insecure-http-parser "
            .nodejs_terminate_on_debug_end = True
            
        End With
        
        CommandLineSwitches = CommandLineSwitches + " --no-proxy-server --disable-cache --disable-gpu-program-cache --disable-gpu-shader-disk-cache"
        CommandLineSwitches = CommandLineSwitches + " --disable-background-networking --disable-background-timer-throttling --disable-backgrounding-occluded-windows"
        CommandLineSwitches = CommandLineSwitches + " --disable-breakpad --disable-client-side-phishing-detection --disable-default-apps --disable-dev-shm-usage "
        CommandLineSwitches = CommandLineSwitches + " --disable-renderer-backgrounding --disable-sync --metrics-recording-only --no-first-run --no-default-browser-check"
        
    End Sub
    Option Explicit
    
    Private FS As New FileSystemObject
    
    Private ClientDebuggerReady As Boolean
    Private ServerDebuggerReady As Boolean
    
    Public ClientPaused As Boolean
    Public ServerPaused As Boolean
    
    Public PageLoaded As Boolean
    
    Private XDOM As Object
    
    Public WithEvents DebugBridge As DebugBridge
    
    ' This method is called from Editor_OnComPostMessage when user clicks Play in CRUDPad HTML
    Public Sub LoadPage(ByVal XML As String)
        
        On Error Resume Next
        
        If XML = "" Then Exit Sub
        Set XDOM = XML_DOM(XML)
        
        Caption = "Starting client and server debuggers, please wait ..."
        StartServer
        StartClient
        
        Show
        
    End Sub
    
    Public Sub StartClient()
    
        On Error Resume Next
        
        If XDOM Is Nothing Then Exit Sub
            
        Caption = "Loading " + XDOM.selectSingleNode("JSON/HTML_URL").Text
        
        LoadBreakPoints DEBUGGER_CHROMIUM
    
        WebKitX.BaseURL = XDOM.selectSingleNode("JSON/BASE_URL").Text
        WebKitX.Open XDOM.selectSingleNode("JSON/HTML_URL").Text + ""
        
    End Sub
    
    Public Sub StartServer()
    
        On Error Resume Next
        
        If XDOM Is Nothing Then Exit Sub
        
        WebKitX.StartNodeJS XDOM.selectSingleNode("JSON/SERVER_SCRIPT_FILE").Text
    
    End Sub
    
    Public Sub LoadBreakPoints(Debugger As WEBKITX_DEBUGGER)
    
        On Error Resume Next
        
        If XDOM Is Nothing Then Exit Sub
        
        Dim URL As String
        Dim BreakPoint As IXMLDOMElement
        
        If Debugger = DEBUGGER_CHROMIUM Then
                    
            ' Load client breakpoints
            URL = XDOM.selectSingleNode("JSON/CLIENT_SCRIPT_URL").Text
            For Each BreakPoint In XDOM.selectNodes("JSON/CLIENT_BREAKPOINTS/item")
                WebKitX.DebuggerAddBreakpoint DEBUGGER_CHROMIUM, URL, CLng(BreakPoint.Text) - 1, 0
            Next
        
        End If
        
        If Debugger = DEBUGGER_NODEJS Then
                
            ' Load server breakpoints
            URL = XDOM.selectSingleNode("JSON/SERVER_SCRIPT_URL").Text
            For Each BreakPoint In XDOM.selectNodes("JSON/SERVER_BREAKPOINTS/item")
                WebKitX.DebuggerAddBreakpoint DEBUGGER_NODEJS, URL, CLng(BreakPoint.Text) - 1, 0
            Next
            
            WebKitX.DebuggerContinue DEBUGGER_NODEJS
                
        End If
        
        UpdateDebuggerStatus
        
    End Sub
    
    ' ==================================================================================================================================
    '       ______
    '      / ____/___  _________ ___
    '     / /_  / __ \/ ___/ __ `__ \
    '    / __/ / /_/ / /  / / / / / /
    '   /_/    \____/_/  /_/ /_/ /_/
    '
    ' ==================================================================================================================================
    
    Private Sub Form_Load()
        
        On Error Resume Next
        
        Set DebugBridge = CreateObject("CRUDPadDebugBridge.DebugBridge")
        
    End Sub
    
    Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
        On Error Resume Next
        If UnloadMode = 0 Then
            Cancel = 1
            frm_Designer.Editor.DispatchEventAsync "#x-w2ui-designer-action-stop", "click", False, False, False, ""
        End If
    End Sub
    
    Private Sub Form_Resize()
        
        On Error Resume Next
        
        WebKitX.Move 0, 0, ScaleWidth, ScaleHeight
        
    End Sub
    
    ' ==================================================================================================================================
    '       ____       __                   ____       _     __
    '      / __ \___  / /_  __  ______ _   / __ )_____(_)___/ /___ ____
    '     / / / / _ \/ __ \/ / / / __ `/  / __  / ___/ / __  / __ `/ _ \
    '    / /_/ /  __/ /_/ / /_/ / /_/ /  / /_/ / /  / / /_/ / /_/ /  __/
    '   /_____/\___/_.___/\__,_/\__, /  /_____/_/  /_/\__,_/\__, /\___/
    '                          /____/                      /____/
    ' ==================================================================================================================================
    
    Private Sub DebugBridge_OnEval(ByVal DebuggerHost As Long, ByVal Expression As String, ByVal CallFrameID As String, ByVal Silent As Boolean, ByVal ReturnByValue As Boolean, ByVal GeneratePreview As Boolean, Result As String)
        
        On Error Resume Next
        
        If DebuggerHost = DEBUGGER_CHROMIUM And ClientDebuggerReady = False Then Exit Sub
        If DebuggerHost = DEBUGGER_NODEJS And ServerDebuggerReady = False Then Exit Sub
        
        Result = WebKitX.DebuggerEvaluate(DebuggerHost, Expression, CallFrameID, Silent, ReturnByValue, GeneratePreview)
        
    End Sub
    
    Private Sub DebugBridge_OnProperties(ByVal DebuggerHost As Long, ByVal ObjectID As String, Result As String)
        
        On Error Resume Next
        
        If DebuggerHost = DEBUGGER_CHROMIUM And ClientDebuggerReady = False Then Exit Sub
        If DebuggerHost = DEBUGGER_NODEJS And ServerDebuggerReady = False Then Exit Sub
        
        Result = WebKitX.DebuggerGetProperties(DebuggerHost, ObjectID)
        
    End Sub
    
    Private Sub DebugBridge_OnGetCode(ByVal DebuggerHost As Long, ByVal ScriptID As Long, Result As String)
        
        On Error Resume Next
        
        Result = WebKitX.DebuggerGetScriptSource(DebuggerHost, ScriptID)
        
    End Sub
    
    ' ==================================================================================================================================
    '    _       __     __    __ __ _ __ _  __
    '   | |     / /__  / /_  / //_/(_) /| |/ /
    '   | | /| / / _ \/ __ \/ ,<  / / __/   /
    '   | |/ |/ /  __/ /_/ / /| |/ / /_/   |
    '   |__/|__/\___/_.___/_/ |_/_/\__/_/|_|
    '
    ' ==================================================================================================================================
    
    Private Sub WebKitX_OnCreate(ByVal Settings As WebKitXCEF3Lib.ISettings, CommandLineSwitches As String)
        
        On Error Resume Next
        
        With Settings
            
            .app_path = App.Path
            .accept_language_list = "es-CL,en-GB"
            .remote_debugging_port = 9999
            .persist_session_cookies = 0
            .persist_user_preferences = 0
            
            .nodejs_auto_start = False
            .nodejs_debug_port = 9998
            .nodejs_debugger_protocol = "inspector"
            .nodejs_executable = App.Path + "\etc\config\server\node.exe"
            .nodejs_modules_path = App.Path + "\etc\config\server\node_modules"
            .nodejs_command_line_arguments = " --stack-trace-limit=25 --interactive --report-compact --insecure-http-parser "
            .nodejs_terminate_on_debug_end = True
            
        End With
        
        CommandLineSwitches = CommandLineSwitches + " --no-proxy-server --disable-cache --disable-gpu-program-cache --disable-gpu-shader-disk-cache"
        CommandLineSwitches = CommandLineSwitches + " --disable-background-networking --disable-background-timer-throttling --disable-backgrounding-occluded-windows"
        CommandLineSwitches = CommandLineSwitches + " --disable-breakpad --disable-client-side-phishing-detection --disable-default-apps --disable-dev-shm-usage "
        CommandLineSwitches = CommandLineSwitches + " --disable-renderer-backgrounding --disable-sync --metrics-recording-only --no-first-run --no-default-browser-check"
        
    End Sub
    
    Private Sub WebKitX_OnDebuggerStarted(ByVal Debugger As WebKitXCEF3Lib.WEBKITX_DEBUGGER)
        
        On Error Resume Next
        
        If Debugger = DEBUGGER_CHROMIUM Then
            
            ClientDebuggerReady = True
            
        End If
        
        If Debugger = DEBUGGER_NODEJS Then
            ServerDebuggerReady = True
            LoadBreakPoints DEBUGGER_NODEJS
        End If
        
        LoadBreakPoints Debugger
        
        UpdateDebuggerStatus
        
    End Sub
    
    Private Sub WebKitX_OnLoadEnd()
        
        On Error Resume Next
        
        Caption = WebKitX.Document.Title
        PageLoaded = True
        UpdateDebuggerStatus
        
    End Sub
    
    Private Sub WebKitX_OnDebuggerTerminated(ByVal Debugger As WebKitXCEF3Lib.WEBKITX_DEBUGGER)
        
        On Error Resume Next
        
        If Debugger = DEBUGGER_CHROMIUM Then
            ClientDebuggerReady = False
            
       ElseIf Debugger = DEBUGGER_NODEJS Then
            ServerDebuggerReady = False
            
        End If
    
    End Sub
    
    Private Sub WebKitX_OnWebSocketError(ByVal Debugger As WebKitXCEF3Lib.WEBKITX_DEBUGGER, ByVal ErrorCode As Long, ByVal ErrorDescr As String)
    
        On Error Resume Next
        
        If Debugger = DEBUGGER_CHROMIUM Then
            ClientDebuggerReady = False
        
        ElseIf Debugger = DEBUGGER_NODEJS Then
            ServerDebuggerReady = False
            
        End If
    
    End Sub
    
    Private Sub WebKitX_OnDebuggerPaused(ByVal Debugger As WebKitXCEF3Lib.WEBKITX_DEBUGGER)
        
        On Error Resume Next
        
        If Debugger = DEBUGGER_CHROMIUM Then
            ClientPaused = True
        
        ElseIf Debugger = DEBUGGER_NODEJS Then
            ServerPaused = True
                
        End If
        
        UpdateDebuggerStatus
        
    End Sub
    
    Private Sub WebKitX_OnDebuggerResumed(ByVal Debugger As WebKitXCEF3Lib.WEBKITX_DEBUGGER)
        
        On Error Resume Next
        
        If Debugger = DEBUGGER_CHROMIUM Then
            ClientPaused = False
            
        ElseIf Debugger = DEBUGGER_NODEJS Then
            ServerPaused = False
            
        End If
        
        UpdateDebuggerStatus
            
    End Sub
    
    Private Sub WebKitX_OnDebuggerBreakpointHit(ByVal Debugger As WebKitXCEF3Lib.WEBKITX_DEBUGGER, _
                                                ByVal URL As String, ByVal ScriptID As String, ByVal CallFrameID As String, _
                                                ByVal Line As Long, ByVal Col As Long, ByVal Exception As String, _
                                                ByVal JSON As String)
        On Error Resume Next
        
        frm_Designer.Editor.CallByNameAsync "OnDebuggerBreakpointHit", Array(Debugger, URL, ScriptID, CallFrameID, Line, Col, Exception)
        
        UpdateDebuggerStatus
        
    End Sub
    
    Private Sub WebKitX_OnConsoleMessage(ByVal ConsoleMessage As String, ByVal Source As String, ByVal Line As Long)
        
        On Error Resume Next
        
        frm_Designer.Editor.CallByNameAsync "OnDebuggerConsoleMessage", Array(ConsoleMessage, Source, Line)
        
    End Sub
    
    Private Sub WebKitX_OnError(ByVal ErrorCode As Long, ByVal ErrorDescr As String)
        
        On Error Resume Next
        
        frm_Designer.Editor.CallByNameAsync "OnDebuggerError", Array(ErrorDescr, ErrorCode)
        
    End Sub
    
    Private Sub UpdateDebuggerStatus()
        
        Dim msg As String
        
        If PageLoaded Then
            msg = IIf(ClientPaused, "Client is Paused", "Client is Running") + " | " + IIf(ServerPaused, "Server is Paused", "Server is Running")
        Else
            msg = "Ready"
        End If
        
        frm_Designer.PrintStatus msg
    
    End Sub
    See Also