Remote NodeJS

Just starting out? Need help? Post your questions and find answers here.
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5357
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Remote NodeJS

Post by Kwai chang caine »

Hello at all

Thanks from an original code of INFRATEC for PhantomJS,
viewtopic.php?p=481800#p481800
I try to remote the STDOUT of NodeJS, and obviously, if i write here...that not works :oops:

In fact that work in script mode or even in eval script mode

Code: Select all

*ThreadParameter\ProgramID = RunProgram(CheminNodeJS$ + "node.exe", "-e " + Chr(34) + "process.stdout.write('Kcc')" + Chr(34), CheminNodeJS$, #PB_Program_Open|#PB_Program_Read|#PB_Program_Write|#PB_Program_Error|#PB_Program_UTF8);
but not in REPL mode :|

Code: Select all

;*ThreadParameter\ProgramID = RunProgram(CheminNodeJS$ + "node.exe", "", "", #PB_Program_Open|#PB_Program_Read|#PB_Program_Write|#PB_Program_Error|#PB_Program_UTF8);
Apparently it's strange like behavior but it's possible to read the REPL console
https://stackoverflow.com/questions/349 ... andard-out
> var a = "lol";
undefined
> process.stdout.write(a);
loltrue
But nothing out for me :|

Code: Select all

; [Infratec] https://www.purebasic.fr/english/viewtopic.php?p=481800#p481800

CompilerIf Not #PB_Compiler_Thread
 MessageRequester("Info", "Enable Thread-Safe in compiler options!")
 End
CompilerEndIf

Enumeration
 #Form0
 #EditorCommande
 #StringCommande
 #Bouton
EndEnumeration

Enumeration #PB_Event_FirstCustomValue
 #Own_Event_FromProg
 #Own_Event_Exit
EndEnumeration

Structure ThreadParameterStructure
 Semaphore.i
 Thread.i
 ProgramID.i
 FromProg$
 ToProg$
 Exit.i
EndStructure

Global CheminNodeJS$ = GetPathPart(ProgramFilename()) + "NodeJs8.12.0\"
Global Event.i, Exit.i
Global ThreadParameter.ThreadParameterStructure

Procedure HandleIO(*ThreadParameter.ThreadParameterStructure)
 
 Protected ReadLen.i, WriteLen.i, Error$
 
 *Buffer = AllocateMemory(1024)
 
 If *Buffer
  
  ;*ThreadParameter\ProgramID = RunProgram(CheminNodeJS$ + "node.exe", "", "", #PB_Program_Open|#PB_Program_Read|#PB_Program_Write|#PB_Program_Error|#PB_Program_UTF8);|#PB_Program_Hide)
  *ThreadParameter\ProgramID = RunProgram(CheminNodeJS$ + "node.exe", "-e " + Chr(34) + "process.stdout.write('Kcc')" + Chr(34), CheminNodeJS$, #PB_Program_Open|#PB_Program_Read|#PB_Program_Write|#PB_Program_Error|#PB_Program_UTF8);
    
  If *ThreadParameter\ProgramID
   
   Repeat
    
    ReadLen = AvailableProgramOutput(*ThreadParameter\ProgramID)
    
    If ReadLen
    
     ReadLen = ReadProgramData(*ThreadParameter\ProgramID, *Buffer, ReadLen)
     
     If ReadLen
      *ThreadParameter\FromProg$ = PeekS(*Buffer, ReadLen, #PB_UTF8)
      PostEvent(#Own_Event_FromProg)
      WaitSemaphore(*ThreadParameter\Semaphore)
     EndIf
     
    EndIf
    
    Error$ = ReadProgramError(*ThreadParameter\ProgramID, #PB_UTF8)
    
    If Len(Error$)
     *ThreadParameter\FromProg$ = Error$
     PostEvent(#Own_Event_FromProg)
     WaitSemaphore(*ThreadParameter\Semaphore)
    EndIf
    
    If Len(*ThreadParameter\ToProg$)
    
     *ThreadParameter\ToProg$ + #CRLF$
     WriteLen = PokeS(*Buffer, *ThreadParameter\ToProg$, -1, #PB_UTF8)
     *ThreadParameter\ToProg$ = ""
     WriteLen = WriteProgramData(*ThreadParameter\ProgramID, *Buffer, WriteLen)
     SignalSemaphore(*ThreadParameter\Semaphore)
     
    EndIf
    
    Delay(10)
    
   Until *ThreadParameter\Exit Or Not ProgramRunning(*ThreadParameter\ProgramID)
   
   CloseProgram(*ThreadParameter\ProgramID)
   
  EndIf
  
  FreeMemory(*Buffer)
 EndIf
 
 PostEvent(#Own_Event_Exit)
 
EndProcedure

OpenWindow(#Form0, 379, 176, 608, 542, "Remote NodeJS", #PB_Window_SystemMenu|#PB_Window_SizeGadget|#PB_Window_TitleBar)
EditorGadget(#EditorCommande, 5, 15, 593, 447)
StringGadget(#StringCommande, 6, 469, 594, 23, "process.stdout.write('Kcc');")
ButtonGadget(#Bouton, 211, 503, 201, 33, "Send to NodeJS")

ThreadParameter\Semaphore = CreateSemaphore()
ThreadParameter\Thread = CreateThread(@HandleIO(), @ThreadParameter)

Repeat
 
 Event = WaitWindowEvent()
 
 Select Event
 
  Case #Own_Event_FromProg
  
   AddGadgetItem(#EditorCommande, -1, ThreadParameter\FromProg$)
   SignalSemaphore(ThreadParameter\Semaphore)
   
  Case #Own_Event_Exit
  
  ; Exit = #True
   
  Case #PB_Event_Gadget
  
   Select EventGadget()
    Case #Bouton
     ThreadParameter\ToProg$ = GetGadgetText(#StringCommande)
     WaitSemaphore(ThreadParameter\Semaphore)
   EndSelect
   
  Case #PB_Event_CloseWindow
  
   If IsThread(ThreadParameter\Thread)
    CloseProgram(ThreadParameter\ProgramID)
   EndIf
   
   FreeSemaphore(ThreadParameter\Semaphore)
   Exit = #True
   
 EndSelect
 
Until Exit
Perhaps someone know why ?

Have a good day
ImageThe happiness is a road...
Not a destination
zefiro_flashparty
User
User
Posts: 74
Joined: Fri Mar 04, 2005 7:46 pm
Location: argentina

Re: Remote NodeJS

Post by zefiro_flashparty »

Humm i use nodejs to launch telegram bots. Let me sear an example. But need see the path from directory. And meybe need administrator request to work.
Amd Vishera fx8350 ,16Gbram, Gtx650 ti, 2gb,Win 10pro. 13tbs. 8)
zefiro_flashparty
User
User
Posts: 74
Joined: Fri Mar 04, 2005 7:46 pm
Location: argentina

Re: Remote NodeJS

Post by zefiro_flashparty »

Code: Select all

;--IMPORTANT
;u need the folder "node_modules" whith telegram plugin u other plugin in same folder
;-this compiled, need request from administrator mode, in compiler to work in w10 and ohter.
;-the path of directory if necesary.

 Directory$=GetCurrentDirectory();--- important to launch node.exe
 archiveo$="foto.jpg";--name of picture to send in telegram

 
 ;(example if not have the .js from nodejs i make it, in this case to send a picture in telegram.
 ; if u have a .js from other plugin use it or make it.. 
If FileSize("fototem.js")<1
  OpenFile (0,Directory$+"fototem.js")
  WriteStringN(0,"const TelegramBot = require('node-telegram-bot-api');")
  WriteStringN(0,"const token = 'put token here';")
  WriteStringN(0,"const bot = new TelegramBot(token, {polling: false});")
  WriteStringN(0,"const request = require('request');")
  WriteStringN(0,"const chatId =CHATIDHERE")
  WriteStringN(0,"bot.sendMessage(chatId, 'envio de imagen :D');")
  WriteStringN(0,"bot.sendPhoto(chatId,'"+archiveo$+"' );")
  CloseFile(0)  
EndIf


;-launcher
  program$="node.exe";
  Debug program$ 
      
  comandos$=" fototem.js";<----- rememeber space
  Debug comandos$
 
;RunProgram(program$ ,  comandos$, Directory$,  #PB_Program_Hide, 0); #PB_Program_Read,0); <-invisible
RunProgram(program$ ,  comandos$, Directory$,  #PB_Program_Read,0);

  Delay(30000)

;-this compiler need request from administrator to work in w10 and ohter.
__________________________________________________
Code tags added
23.10.2018
RSBasic
Amd Vishera fx8350 ,16Gbram, Gtx650 ti, 2gb,Win 10pro. 13tbs. 8)
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5357
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Remote NodeJS

Post by Kwai chang caine »

Hello zefiro_flashparty :D

Thanks for your answer, but my problem is not to sending something to NodeJs, but rather for hook his Input/output :wink:
ImageThe happiness is a road...
Not a destination
Post Reply