Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ali Mini Program not work in v4.2.0 #1140

Closed
ghost opened this issue Aug 13, 2020 · 5 comments · Fixed by #1187
Closed

Ali Mini Program not work in v4.2.0 #1140

ghost opened this issue Aug 13, 2020 · 5 comments · Fixed by #1187

Comments

@ghost
Copy link

ghost commented Aug 13, 2020

In PR #1135 change index.js and add

 typeof __webpack_require__ === 'function'
// eslint-disable-next-line camelcase
if ((typeof process !== 'undefined' && process.title !== 'browser') || typeof __webpack_require__ === 'function') {
  protocols.mqtt = require('./tcp')
  protocols.tcp = require('./tcp')
  protocols.ssl = require('./tls')
  protocols.tls = require('./tls')
  protocols.mqtts = require('./tls')
} else {
  protocols.wx = require('./wx')
  protocols.wxs = require('./wx')

  protocols.ali = require('./ali')
  protocols.alis = require('./ali')
}

I found that

 typeof __webpack_require__ === 'function'

in Ali Mini Program is true,so will not go else conditional branch to require correct protocol file(ali.js) than an error occur

TypeError: net.createConnection is not a function
    at Object.streamBuilder (mqtt.js:1737)
    at MqttClient.wrapper [as streamBuilder] (mqtt.js:2388)
    at MqttClient._setupStream (mqtt.js:300)
    at new MqttClient (mqtt.js:279)
    at connect (mqtt.js:2391)
    at Object.doConnect (tasks.js:76)
    at Object.onTap (tasks.js:50)

AB#8667518

@YoDaMa
Copy link
Contributor

YoDaMa commented Aug 24, 2020

release 4.2.1 (#1149) should fix this.

@YoDaMa YoDaMa closed this as completed Aug 24, 2020
@YoDaMa YoDaMa reopened this Aug 24, 2020
@YoDaMa
Copy link
Contributor

YoDaMa commented Aug 24, 2020

apologies unrelated

@ghost
Copy link
Author

ghost commented Aug 27, 2020

version v4.2.1 not work well too,the same error occur.

TypeError: net.createConnection is not a function
    at Object.streamBuilder (mqtt.js:1737)
    at MqttClient.wrapper [as streamBuilder] (mqtt.js:2388)
    at MqttClient._setupStream (mqtt.js:300)
    at new MqttClient (mqtt.js:279)
    at connect (mqtt.js:2391)
    at Object.doConnect (tasks.js:76)
    at Object.onTap (tasks.js:50)

Now i have to download the source code then remove the codition

typeof __webpack_require__ === 'function'

and according to PR #1057 to fix other error,it will work well on real phone.So there any other best way to solve this problem?

@ZenDay
Copy link

ZenDay commented Aug 28, 2020

In fact, at PR #1135

// eslint-disable-next-line camelcase
if ((typeof process !== 'undefined' && process.title !== 'browser') || typeof __webpack_require__ === 'function') {
  protocols.mqtt = require('./tcp')
  protocols.tcp = require('./tcp')
  protocols.ssl = require('./tls')
  protocols.tls = require('./tls')
  protocols.mqtts = require('./tls')
}

the codition here is to judge if mqtt is NOT used in browser, because the code at ./tcp and ./tls use the nodejs library.

However typeof __webpack_require__ === 'function' is to judge if it's used in browser. It will cause all the webpack app which use mqtt over v4.2.0(and 4.2.1) throw an TypeError

Uncaught TypeError: net.createConnection is not a function

Here's the correction:

// eslint-disable-next-line camelcase
if ((typeof process === 'undefined' || process.title !== 'browser') && typeof __webpack_require__ !== 'function') {
  protocols.mqtt = require('./tcp')
  protocols.tcp = require('./tcp')
  protocols.ssl = require('./tls')
  protocols.tls = require('./tls')
  protocols.mqtts = require('./tls')
}

Just like the code at the same PR:

// eslint-disable-next-line camelcase
var IS_BROWSER = (typeof process !== 'undefined' && process.title === 'browser') || typeof __webpack_require__ === 'function'

@ghost
Copy link
Author

ghost commented Sep 1, 2020

In fact, at PR #1135

// eslint-disable-next-line camelcase
if ((typeof process !== 'undefined' && process.title !== 'browser') || typeof __webpack_require__ === 'function') {
  protocols.mqtt = require('./tcp')
  protocols.tcp = require('./tcp')
  protocols.ssl = require('./tls')
  protocols.tls = require('./tls')
  protocols.mqtts = require('./tls')
}

the codition here is to judge if mqtt is NOT used in browser, because the code at ./tcp and ./tls use the nodejs library.

However typeof __webpack_require__ === 'function' is to judge if it's used in browser. It will cause all the webpack app which use mqtt over v4.2.0(and 4.2.1) throw an TypeError

Uncaught TypeError: net.createConnection is not a function

Here's the correction:

// eslint-disable-next-line camelcase
if ((typeof process === 'undefined' || process.title !== 'browser') && typeof __webpack_require__ !== 'function') {
  protocols.mqtt = require('./tcp')
  protocols.tcp = require('./tcp')
  protocols.ssl = require('./tls')
  protocols.tls = require('./tls')
  protocols.mqtts = require('./tls')
}

Just like the code at the same PR:

// eslint-disable-next-line camelcase
var IS_BROWSER = (typeof process !== 'undefined' && process.title === 'browser') || typeof __webpack_require__ === 'function'

yes , you are right

nosovk added a commit to nosovk/MQTT.js that referenced this issue Sep 2, 2020
@nosovk nosovk mentioned this issue Sep 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants