2 * Copyright (c) 2012, Intel Corporation.
4 * This program is licensed under the terms and conditions of the
5 * Apache License, version 2.0. The full text of the Apache License is at
6 * http://www.apache.org/licenses/LICENSE-2.0
10 var wsAPI = (function(){
12 var callbackQueue = {};
19 url = typeof url !== 'undefined' ? url : 'ws://localhost:9999';
20 ws = new WebSocket(url);
21 ws.onopen = function() {
28 ws.onmessage = function (e) {
29 console.log('Received data: ', e.data);
30 jsonMsg = JSON.parse(e.data);
31 if (jsonMsg.api_namespace !== 'tizen.ivi.texttospeech') return;
33 if (jsonMsg.type === 'api_error')
34 console.log(jsonMsg.api_error_msg);
36 if (jsonMsg.type === 'method_reply' && jsonMsg.method_name === 'speak') {
37 callback = callbackQueue[jsonMsg.request_id]
38 if (!callback || !typeof(callback) === 'function') {
42 callback(jsonMsg.results);
43 delete callbackQueue[jsonMsg.request_id];
47 ws.onclose = function(e) {
55 console.log('Websocket connection not ready, cannot send message');
58 jsonMsg = JSON.stringify(msg);
60 console.log('sent message: ' + jsonMsg);
63 function invokeMethod(json, callback) {
66 callbackQueue[reqId.toString()] = callback;
67 json['request_id'] = reqId.toString();
69 console.log('invoke method');
77 speak: function (text, callback) {
80 if (callback && typeof(callback) !== 'function')
84 'api_namespace': 'tizen.ivi.texttospeech',
86 'method_name': 'speak',
91 invokeMethod(json, callback);