1

I'm using Typescript and have a class. within that class I have a constructor and method, but I cant seem to call that method within the body of the function:

import  { ParentPlayer } from './parentPlayer'
import ws = require('ws')

export class MasterSocket{

    constructor(masterPlayer:ParentPlayer, serverPort:number, Notifier:any) {
       createPipeline(masterPlayer,serverPort,Notifier)
    }

    function createPipeline(masterPlayer:ParentPlayer, serverPort:number, Notifier:any){
    if(masterPlayer !== null)
      {
        const wss:ws.Server = new ws.Server({ port: serverPort})
        wss.on('connection', function connection(svr) {
            Notifier.emit('test','Pipeline established')

            svr.onmessage = (msg) =>
            {
                Notifier.emit('test','Message received at Pipeline: ' + msg)
            }

            svr.onclose = (evt) =>
            {
                Notifier.emit('test','Backend: Pipeline Closed: ' + evt.reason)
     >>>>>>>    createPipeline(masterPlayer,serverPort,Notifier)
            }

            svr.on('message', function incoming(message) {
            Notifier.emit('test','message received from: ' + message)
            })

          svr.send('this is a message sent from the Pipeline');
        })
      }
    }
}

any help would be greatly appreciated

0

1 Answer 1

2

As peinearydevelopment has already mentioned, you need to remove the function keyword in order for createPipeline to be an actual method of the class.

But you also need to convert your onConnection callback to an arrow function in order to preserve the this context of the class (more on this here):

wss.on('connection', connection(svr) => {
    // ...

    svr.onclose = (evt) => {
        Notifier.emit('test', 'Backend: Pipeline Closed: ' + evt.reason)
        this.createPipeline(masterPlayer, serverPort, Notifier)
    }
    //...
});
Sign up to request clarification or add additional context in comments.

1 Comment

Okay this is what worked...thank you: wss.on('connection', svr => {

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.