iink SDK Web

Myscript websocket server connection problem

I want to connect my local socket server to my script socket server.


here is my code: 

import WebSocket, { WebSocketServer } from "ws";
import http from "http";

const server = http.createServer();
const wss = new WebSocketServer({ server, path: "/api/v4.0/iink/document" });

wss.on("connection", (ws, ) => {
  console.log("Client connected");

  // Event handler for messages from the client
  ws.on("message", (message) => {
    const iws = new WebSocket(
      "wss://webdemoapi.myscript.com/api/v4.0/iink/document"
    );

    iws.on("error", (error) => {
      console.error("WebSocket error:", error);
    });

    iws.on("open", () => {
      console.log("WebSocket connection opened");
      console.log(JSON.parse(message.toString()));
      iws.send(message);
    });

    iws.on("message", (msg) => {
      
      ws.send(msg.toString());
    });

    iws.on("close", () => {
      console.log("WebSocket connection closed");
    });
  });

  // Event handler for closing the connection
  ws.on("close", () => {
    console.log("Client disconnected");
  });
});

const port = 8080;

server.listen(port, () => {
  console.log(`WebSocket server is listening on port ${port}`);
});

 

when I send a message using the Postman WebSocket client, it's connected to the server and gets back a response. see the below image.


 

{
  "type": "newContentPackage",
  "applicationKey":  "XXXX-XXXXXX-XXXXX-XXXX",
  "xDpi": 90,
  "yDpi": 90,
  "viewSizeHeight": 800,
  "viewSizeWidth": 1023
}

 


image

 



after sending an HMAC message, I get the error for my server.


this is the javascript code for computing the HMAC code. 

import pkg from 'crypto-js';
const {HmacSHA512, Hex} = pkg;



const APPLICATIONKEY = ""
const HMACKEY = ""

export function computeHmac (input, applicationKey= APPLICATIONKEY, hmacKey = HMACKEY) {
  const jsonInput = (typeof input === 'object') ? JSON.stringify(input) : input

  return new HmacSHA512(jsonInput, applicationKey + hmacKey).toString(Hex)
}



console.log(computeHmac("cbfca330-e1ce-4b51-8afe-d2763c36e3ab"));



 

{
  "type": "hmac",
  "hmac": "ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"
}

  
Error:

image

image




but when I connect direct Myscript server ->  wss://webdemoapi.myscript.com/api/v4.0/iink/document

then the same HMAC code and other things work perfectly, not getting any errors.

image




So where is my problem? how can solve the issue? or How do I connect my WebSocket server to my script cloud socket server?



Dear Biplob,


Thank you for contacting us.


First, we would like to make two remarks about application keys:

  1.  They are personal information that you should keep private and do not share them publicly
  2.  The keys that you are using are the demo ones, that you should not use for your project: for your project, please use you own application key.


We are not sure to understand what you are trying to achieve and what you mean by "connect my WebSocket server to my script cloud socket server"

For a Web Socket based project we recommend you using the iinkTS library that makes the integration of handwriting recognition and interactive ink easier in any web application.


Then you can integrate your application and deploy it by your own web server as described in the https://developer.myscript.com/getting-started/web page.


Please, share with us more context on what you are trying to do, as we don't undertand why you want to use a WebSocket server 


Best regards,


Gwenaëlle




Thank you Gwenaëlle for your reply. My front-end application uses the iinkJS library with WebSocket protocol. So now I need my frontend application request first will go to my backend server. Where I have some business logic, then I will connect my backend web socket server to my script web socket server based on my business logic. So I want to achieve, iinkJS library -> my socket server -> my script web socket server. I know there are also Rest APIs available. And I can do that very easily with rest API. But my need is the web socket protocol. How can I connect my script web socket server from another websocket server?

Dear Biplob,


Thank you for your update.

We don't have any experience of such an integration nor any example to share with you.

If you encounter Access not granted error, it is likely due to your hmac computation that is not correct: either the way you compute it or the data on which you compute it.


You may refer to this post that illustrates such a computation in javascript : https://developer-support.myscript.com/support/discussions/topics/16000031800

or to the iink-ts source code: https://github.com/MyScript/iinkTS/blob/master/src/utils/crypto.ts and  https://github.com/MyScript/iinkTS/blob/master/src/recognizer/WSRecognizer.ts


Best regards,


Gwenaëlle

Login or Signup to post a comment