��权限没有,则隐藏 function forum_list_access_filter($forumlist, $gid, $allow = 'allowread') { global $grouplist; if (empty($forumlist)) return array(); if (1 == $gid) return $forumlist; $forumlist_filter = $forumlist; $group = $grouplist[$gid]; foreach ($forumlist_filter as $fid => $forum) { if (empty($forum['accesson']) && empty($group[$allow]) || !empty($forum['accesson']) && empty($forum['accesslist'][$gid][$allow])) { unset($forumlist_filter[$fid]); } unset($forumlist_filter[$fid]['accesslist']); } return $forumlist_filter; } function forum_filter_moduid($moduids) { $moduids = trim($moduids); if (empty($moduids)) return ''; $arr = explode(',', $moduids); $r = array(); foreach ($arr as $_uid) { $_uid = intval($_uid); $_user = user_read($_uid); if (empty($_user)) continue; if ($_user['gid'] > 4) continue; $r[] = $_uid; } return implode(',', $r); } function forum_safe_info($forum) { //unset($forum['moduids']); return $forum; } function forum_filter($forumlist) { foreach ($forumlist as &$val) { unset($val['brief'], $val['announcement'], $val['seo_title'], $val['seo_keywords'], $val['create_date_fmt'], $val['icon_url'], $val['modlist']); } return $forumlist; } function forum_format_url($forum) { global $conf; if (0 == $forum['category']) { // 列表URL $url = url('list-' . $forum['fid'], '', FALSE); } elseif (1 == $forum['category']) { // 频道 $url = url('category-' . $forum['fid'], '', FALSE); } elseif (2 == $forum['category']) { // 单页 $url = url('read-' . trim($forum['brief']), '', FALSE); } if ($conf['url_rewrite_on'] > 1 && $forum['well_alias']) { if (0 == $forum['category'] || 1 == $forum['category']) { $url = url($forum['well_alias'], '', FALSE); } elseif (2 == $forum['category']) { // 单页 $url = ($forum['threads'] && $forum['brief']) ? url($forum['well_alias'] . '-' . trim($forum['brief']), '', FALSE) : url($forum['well_alias'], '', FALSE); } } return $url; } function well_forum_alias() { $forumlist = forum_list_cache(); if (empty($forumlist)) return ''; $key = 'forum-alias'; static $cache = array(); if (isset($cache[$key])) return $cache[$key]; $cache[$key] = array(); foreach ($forumlist as $val) { if ($val['well_alias']) $cache[$key][$val['fid']] = $val['well_alias']; } return array_flip($cache[$key]); } function well_forum_alias_cache() { global $conf; $key = 'forum-alias-cache'; static $cache = array(); // 用静态变量只能在当前 request 生命周期缓存,跨进程需要再加一层缓存:redis/memcached/xcache/apc if (isset($cache[$key])) return $cache[$key]; if ('mysql' == $conf['cache']['type']) { $arr = well_forum_alias(); } else { $arr = cache_get($key); if (NULL === $arr) { $arr = well_forum_alias(); !empty($arr) AND cache_set($key, $arr); } } $cache[$key] = empty($arr) ? '' : $arr; return $cache[$key]; } ?>javascript - Using Socket.IO from Node.js to connect to external server - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Using Socket.IO from Node.js to connect to external server - Stack Overflow

programmeradmin4浏览0评论

Background: I have a node.js server running on my localhost (call this Server A); and an external server running node.js at :3000 (call this Server B). I do not control or have access to Server B (it is a dashboard site for an IoT device in my home), but I need to connect to is using socket.io and emit a specific message.

I can connect to it easily from a flat javascript file (client-side), but need it running server side (ultimate goal is to make it into something I can call with an HTTP request); and examples such as How to connect two node.js servers with websockets? suggest I should be able to use socket.io-client from node.js with nearly the same code to achieve the same results. But when I run the code from node.js, I cannot connect to the socket.

Below is the code that works successfully in flat javascript file. I know it works because I see 'socket connect' in the console, and I can also test for the the socket emit at the end.

var myemail = "[email protected]";
var device_id = '12345';
// Create SocketIO instance, connect
var socket = io.connect(':3000');

socket.on('connect', function(){
    try {
        console.log('socket connect');
        socket.emit('configure', {email:myemail, deviceid:device_id});
    } catch(e) {
        console.log(e);
    }
});
socket.emit("/" + device_id, "45678");

...and below is the code I cannot get to work when running from my node.js instance. I'd expect a message 'socket connect' in the mand line log and get nothing.

var express=require('express');
var http=require('http');
var app=express();
var server = http.createServer(app);
//Variables

var myemail = "[email protected]";
var device_id = '12345'; 

var io = require('socket.io-client');
var socket = io.connect(':3000');

//Connect listener
socket.on('connect', function(){
   try {
      console.log('socket connect');
      socket.emit('configure', {email:myemail, deviceid:device_id});
   } catch(e) {
     console.log(e);
   }
});
socket.emit("/" + device_id, "45678");

Any ideas?

UPDATE

Ran debug utility, results included as linked image below. Key thing I see is that engine.io tries to do an xhr poll, and gets a 503 response back from the server. (Obviously not a true 'temporary error' with the server as again, this all works from running client-side js in chrome).

debugging output image link

Background: I have a node.js server running on my localhost (call this Server A); and an external server running node.js at https://example:3000 (call this Server B). I do not control or have access to Server B (it is a dashboard site for an IoT device in my home), but I need to connect to is using socket.io and emit a specific message.

I can connect to it easily from a flat javascript file (client-side), but need it running server side (ultimate goal is to make it into something I can call with an HTTP request); and examples such as How to connect two node.js servers with websockets? suggest I should be able to use socket.io-client from node.js with nearly the same code to achieve the same results. But when I run the code from node.js, I cannot connect to the socket.

Below is the code that works successfully in flat javascript file. I know it works because I see 'socket connect' in the console, and I can also test for the the socket emit at the end.

var myemail = "[email protected]";
var device_id = '12345';
// Create SocketIO instance, connect
var socket = io.connect('https://example:3000');

socket.on('connect', function(){
    try {
        console.log('socket connect');
        socket.emit('configure', {email:myemail, deviceid:device_id});
    } catch(e) {
        console.log(e);
    }
});
socket.emit("/" + device_id, "45678");

...and below is the code I cannot get to work when running from my node.js instance. I'd expect a message 'socket connect' in the mand line log and get nothing.

var express=require('express');
var http=require('http');
var app=express();
var server = http.createServer(app);
//Variables

var myemail = "[email protected]";
var device_id = '12345'; 

var io = require('socket.io-client');
var socket = io.connect('https://example:3000');

//Connect listener
socket.on('connect', function(){
   try {
      console.log('socket connect');
      socket.emit('configure', {email:myemail, deviceid:device_id});
   } catch(e) {
     console.log(e);
   }
});
socket.emit("/" + device_id, "45678");

Any ideas?

UPDATE

Ran debug utility, results included as linked image below. Key thing I see is that engine.io tries to do an xhr poll, and gets a 503 response back from the server. (Obviously not a true 'temporary error' with the server as again, this all works from running client-side js in chrome).

debugging output image link

Share Improve this question edited Aug 21, 2017 at 3:50 Lee asked Aug 20, 2017 at 2:47 LeeLee 811 silver badge5 bronze badges 11
  • Here's a guess. What happens if you want until inside the connect event in order to do your socket.emit("/" + device_id, "45678");. You are attempting that .emit() before the connect has finished. Perhaps this is somehow not causing a problem in the browser, but is on node.js? Timing could be different on the two platforms, perhaps. – jfriend00 Commented Aug 20, 2017 at 3:09
  • Oh, and btw, socket.io-client works just fine in node.js to connect to other socket.io servers so that is not an issue. – jfriend00 Commented Aug 20, 2017 at 3:10
  • FYI, socket.io has various debug modes that will give you very verbose logging on what's going on too. That might elucidate what is happening. – jfriend00 Commented Aug 20, 2017 at 3:14
  • Thanks jfriend00 for the response. Tried adding the emit into the on.("connect") function, didn't work - anyway, it's the initial handshake emit already inside the on connect function that isnt working. ... Also tried using the IP of the server instead of the name. Regarding the debug modes, any thoughts on what i should be looking for? I can see it is not connected but not sure what to look for. – Lee Commented Aug 20, 2017 at 4:09
  • Are you sure you have the right version of socket.io-client on your server to match what you're trying to connect to? socket.io can be picky about matching client and server versions. I don't know the debug info in socket.io to know what to look for. If you want to post all the debug output somewhere, I'll look at it. – jfriend00 Commented Aug 20, 2017 at 4:20
 |  Show 6 more ments

2 Answers 2

Reset to default 4

Solved this - issue was that the server I was connecting to required use of https, so I needed to add

{secure: true,    rejectUnauthorized: false}

after the url to connect to.

Full working example:

const myemail   = [email protected];
const device_id   = 12345;
io = require('socket.io-client');
var socket = io.connect('https://server:3000',{secure: true,    rejectUnauthorized: false});

function doStuff(){


//Listener
socket.on('connect', function(){
   try {
      console.log('socket connect');
      socket.emit('configure', {email:myemail, deviceid:device_id});

   } catch(e) {
     console.log(e);
   }
});

socket.emit("/" + device_id, "003021");


}


doStuff();

I think the line causing the issue is :

var socket = io.connect('https://example:3000');

I managed to make a working example using this code :

const myemail = "[email protected]";
const device_id = '12345'; 
var socket = require('socket.io-client')('https://example:3000');
socket.on('connect', function(){
 try{
   console.log('socket connect');
   socket.emit('configure', {email:myemail, deviceid:device_id});
 }catch(e){ console.log(e); }
});
发布评论

评论列表(0)

  1. 暂无评论