Unity-Technologies / Unity-Technologies/multiplayer-community-contributions

Pointer_stringify -> UTF8ToString

Open
#249 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C#
Stars
555
Forks
197
Avg merge
4d 12h
Merged PRs (30d)
1

Description

I found that the console traces an error saying that the Pointer_stringify method is deprecated. So I updated the file "JSWebSocketClient.jslib".

var LibraryWebSocket = {
  $state: {
    url: null,
    ws: null,
    debug: false,
    onOpen: null,
    onMessage: null,
    onError: null,
    onClose: null,
  },

  _SetUrl: function (urlPointer) {
    state.url = UTF8ToString(urlPointer);
  },

  _SetOnOpen: function (callback) {
    state.onOpen = callback;
  },

  _SetOnMessage: function (callback) {
    state.onMessage = callback;
  },

  _SetOnError: function (callback) {
    state.onError = callback;
  },

  _SetOnClose: function (callback) {
    state.onClose = callback;
  },

  _Connect: function () {
    state.ws = new WebSocket(state.url);
    state.ws.binaryType = 'arraybuffer';

    state.ws.onopen = function () {
      if (state.debug) {
        console.log("[Netcode.WebSocket] Connected.");
      }

      if (state.onOpen) {
        Module['dynCall_v'](state.onOpen);
      }
    };

    state.ws.onmessage = function (ev) {
      if (state.debug) {
        console.log("[Netcode.WebSocket] Received message:", ev.data);
      }

      if (!state.onMessage) {
        return;
      }

      if (ev.data instanceof ArrayBuffer) {
        var dataBuffer = new Uint8Array(ev.data);

        var buffer = _malloc(dataBuffer.length);
        HEAPU8.set(dataBuffer, buffer);

        try {
          Module['dynCall_vii'](state.onMessage, buffer, dataBuffer.length);
        } finally {
          _free(buffer);
        }
      }
    };

    state.ws.onerror = function (ev) {
      if (state.debug) {
        console.log("[Netcode.WebSocket] Error occured.");
      }

      if (state.onError) {
        var msg = "WebSocket error.";
        var msgBytes = lengthBytesUTF8(msg);
        var msgBuffer = _malloc(msgBytes + 1);
        stringToUTF8(msg, msgBuffer, msgBytes);

        try {
          Module['dynCall_vi'](state.onError, msgBuffer)
        } finally {
          _free(msgBuffer);
        }
      }
    };

    state.ws.onclose = function (ev) {
      if (state.debug) {
        console.log("[Netcode.WebSocket] Closed.");
      }

      if (state.onClose) {
        Module['dynCall_vi'](state.onClose, ev.code)
      }
    };
  },

  _Close: function (code, reasonPointer) {
    if (!state.ws) return -3;
    if (state.ws.readyState === 2) return -4;
    if (state.ws.readyState === 3) return -5;

    var reason = (reasonPointer ? UTF8ToString(reasonPointer) : undefined);

    try {
      state.ws.close(code, reason);
    } catch (err) {
      return -7;
    }
  },

  _Send: function (bufferPtr, offset, count) {
    if (!state.ws) return -3;
    if (state.ws.readyState !== 1) return -6;

    state.ws.send(HEAPU8.buffer.slice(bufferPtr + offset, bufferPtr + count - offset));
  },

  _GetState: function () {
    return state.ws ? state.ws.readyState : 3;
  }
};

autoAddDeps(LibraryWebSocket, '$state');
mergeInto(LibraryManager.library, LibraryWebSocket);

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with JSWebSocketClient.jslib and review the Pointer_stringify call sites and the proposed UTF8ToString replacements. Done means the deprecated conversion is no longer used and WebSocket URL and close-reason handling still work; check the related Unity WebSocket behavior after the change.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
networking
Issue type
Refactor
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.