0 Favourites

How do I make a word-cloud a real time?

  • Hi,

    How do I make a WORD CLOUD a real time to other users?

    Here is my sample created using Construct 2.

    https://dl.dropboxusercontent.com/u/519 ... index.html

    When I input a word, it will fall down.

    If other user input a WORD, I can also see the word in real time and save all in an xml or database for future viewing.

    I'm thinking of Node.js but I my knowledge is limited.

    Thanks for any information.

  • Hi Gumshoe2099,

    I tried Nodejs but I am not that good.

    Here's my server code below.

    My problem is that, when I send data to server, I can't get back the data back but it will display in console.

    Any idea?

    var WebSocketServer = require('ws').Server

    , wss = new WebSocketServer({port: 8080});

    wss.on('connection', function(ws) {

    ws.on('message', function(message) {

    console.log('received: %s', message);

    });

    ws.send('something');

    });

  • var WebSocketServer = require('ws').Server

    , wss = new WebSocketServer({port: 8080});

    wss.on('connection', function(ws) {

    ws.on('message', function(message) {

    console.log('received: %s', message);

    });

    ws.send('something');

    });

    The one main problem that I see right off is that you rolled your onmessage method into your onconnection method.

    Those two need to be separate functions, like:

    var WebSocketServer = require('ws').Server, wss = new WebSocketServer({port: 8080});
    
    wss.on('connection', function(ws) {
            ws.send('Connected.');
     });
    
    ws.on('message', function(message) {
            console.log('received: %s', message);
                ws.send('Message received.');
        });
    [/code:2jwc88p6]
    
    You also need to make sure that you have the "Access-Control-Allow-Origin" header set to '*'  because Construct is very particular about security, and your websockets will not work if that header is not set on the server side.
    
    See this example for more info: [url=http://usualcarrot.com/nodejs-and-websocket-simple-chat-tutorial]http://usualcarrot.com/nodejs-and-webso ... t-tutorial[/url]
    
    You can see the compartmentalization in my Java WebSocket server code below:
    [code:2jwc88p6]
    public GalaxyChatEndPoint(){
        	mLog = new MessageLogManager(filePath, fLogger, SP);
        }
        
        /**
         * Callback hook for Connection open events. This method will be invoked when a
         * client requests for a WebSocket connection.
         * 

    param userSession the userSession which is opened.[/p] */[/p] OnOpen[/p] public void onOpen(Session userSession){[/p] try{[/p] userSession.setMaxIdleTimeout(600000);//10 mins[/p] sessionKey = ((PrincipalWithSession) userSession.getUserPrincipal()).getSessionKey();[/p] gameSession = cache.get(sessionKey.getKey());[/p] connections.put(sessionKey, userSession);[/p] if(gameSession==null)[/p] broadcast("<p>gumshoe joined chat.</p>");[/p] else[/p] broadcast(String.format("<p>%s%s</p>", gameSession.getDisplayName()," joined chat."));[/p] }catch(Exception e){[/p] fLogger.error(Errors.err2String(e));[/p] try {[/p] connections.get(sessionKey).close();[/p] connections.remove(sessionKey);[/p] } catch (IOException e1){[/p] fLogger.error(Errors.err2String(e1));[/p] }[/p] }[/p] }[/p] [/p] /**[/p] * Callback hook for Connection close events. This method will be invoked when a[/p] * client closes a WebSocket connection.[/p] * param userSession the userSession which is opened.[/p] */[/p] OnClose[/p] public void onClose() {[/p] try{[/p] if(gameSession==null)[/p] broadcast("<p>gumshoe left chat.</p>");[/p] else[/p] broadcast(String.format("<p>%s%s</p>", gameSession.getDisplayName()," left chat."));[/p] connections.get(sessionKey).close();[/p] connections.remove(sessionKey);[/p] }catch(Exception e){[/p] fLogger.error(Errors.err2String(e));[/p] try {[/p] if(connections.get(sessionKey)!=null)connections.get(sessionKey).close();[/p] if(connections.get(sessionKey)!=null)connections.remove(sessionKey);[/p] } catch (IOException e1){[/p] fLogger.error(Errors.err2String(e1));[/p] }[/p] }[/p] }[/p] [/p] /**[/p] * Callback hook for Message Events. This method will be invoked when a client[/p] * sends a message.[/p] * param message The text message[/p] * param userSession The session of the client[/p] */[/p] OnMessage[/p] public void onMessage(String messageJson) {[/p] try{[/p] Date now = new Date();[/p] String dateFmt = new SimpleDateFormat("EEE MMM HH:mm:ss z yyyy").format(now);[/p] Message message = Message.fromJson(messageJson, gameSession);[/p] globalMessageLog.put(currentMessageId.incrementAndGet(), message);[/p] if(message.getDestination().getDestType()==DestinationType.GALAXY)[/p] if(gameSession==null)[/p] broadcast(String.format(galaxyChatFramework, dateFmt, "gumshoe", message.getContent()));[/p] else[/p] broadcast(String.format(galaxyChatFramework, dateFmt, gameSession.getDisplayName(), message.getContent()));[/p] // this may need to be reworked... perhaps to a raw message number system[/p] Date time1159pm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(new SimpleDateFormat("yyyy-MM-dd").format(now) + " 23:59:58");[/p] [/p] if(now.after(time1159pm)){[/p] if(mLog.writeFile(globalMessageLog, now)){[/p] globalMessageLog.clear();[/p] currentMessageId.set(0);[/p] }[/p] }[/p] }catch(Exception e){[/p] fLogger.error(Errors.err2String(e));[/p] try {[/p] connections.get(sessionKey).close();[/p] connections.remove(sessionKey);[/p] } catch (IOException e1){[/p] fLogger.error(Errors.err2String(e1));[/p] }[/p] }[/p] }[/p] [/p] private static void broadcast(String msg) throws Exception {[/p] for(Session client : connections.values()){[/p] synchronized (client) {[/p] if(client.isOpen()) client.getBasicRemote().sendText(msg);[/p] }[/p] }[/p] }[/p] [/code:2jwc88p6]

  • Hi,

    Sorry, Still I can't make it to work.

    Here is the C2 file that I am currently working on.

    https://dl.dropboxusercontent.com/u/519 ... dcloud.zip

    What I want to achieve is when I input a word, it will also appear on the other browser and vice-versa.

  • I suspect the problem is on your server code, not your Construct2 code. Did you look at that example?

  • Construct 3

    Buy Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Buy Now Construct 3 users don't see these ads
  • hello,

    any chance for a reupload of your wordcloud demo?

    i stumbled over this info here: https://mathematica.stackexchange.com/q ... word-cloud

    https://mathematica.stackexchange.com/q ... ord-clouds

  • After a few month i would like to ask if there is any help on this topic, to convert some wordclouds from SE into c2?

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)