Finology 大数据金融

通过大数据以量化金融

WebSocket Cat (wscat)是一个连接websocket的小工具。

安装方法

1
npm install -g wscat

使用方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Usage: wscat [options] (--listen <port> | --connect <url>)

Options:
-V, --version output the version number
-l, --listen <port> listen on port
-c, --connect <url> connect to a WebSocket server
-p, --protocol <version> optional protocol version
-o, --origin <origin> optional origin
-x, --execute <command> execute command after connecting
-w, --wait <seconds> wait given seconds after executing command
-P, --show-ping-pong print a notification when a ping or pong is received
--host <host> optional host
-s, --subprotocol <protocol> optional subprotocol (default: [])
-n, --no-check do not check for unauthorized certificates
-H, --header <header:value> set an HTTP header. Repeat to set multiple (--connect only) (default: [])
--auth <username:password> add basic HTTP authentication header (--connect only)
--ca <ca> specify a Certificate Authority (--connect only)
--cert <cert> specify a Client SSL Certificate (--connect only)
--key <key> specify a Client SSL Certificate's key (--connect only)
--passphrase [passphrase] specify a Client SSL Certificate Key's passphrase (--connect only). If you don't provide a value, it will be prompted for
--no-color run without color
--slash enable slash commands for control frames (/ping, /pong, /close [code [, reason]])
--proxy <[protocol://]host[:port]> connect via a proxy. Proxy must support CONNECT method
-h, --help

使用案例

在终端1启动一个websocket服务,监听9000端口。

1
2
wscat --listen 9000
Listening on port 9000 (press CTRL+C to quit)

在终端2连接上websocket服务。

1
2
wscat -c ws://localhost:9000/echo
Connected (press CTRL+C to quit)

连上以后,在服务端,会提示信息:

Client connected

然后就可以两端互相发送消息了。

只要客户里面有老外,那都会涉及到多语言国际化的问题。我们看看在 Java Spring Boot 项目中如何设置多语言。

yaml配置

通过查看 org.springframework.boot.autoconfigure.context.MessageSourceAutoConfiguration,我们可以知道需要配置如下属性:spring.messages.basename

1
2
3
spring:
messages:
basename: i18n/message

需要在src\main\resources\i18n目录下添加如下文件:

默认配置文件:message.properties
英文:message_en_US.properties
中文:message_zh_CN.properties

内容都是 key=value 的格式,一行一条。

语言处理类

通过此设置,当请求头里面的Accept-Lanuage没有值时,以中文显示,否则按设置的值来处理。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package cn.com.wind.Wind.RTC.WebService.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver;

import javax.servlet.http.HttpServletRequest;
import java.util.Locale;


@Configuration
public class CustomLocaleResolver extends AcceptHeaderLocaleResolver implements WebMvcConfigurer {

@Override
public Locale resolveLocale(HttpServletRequest request) {

if (!StringUtils.hasText(request.getHeader("Accept-Language"))) {
return Locale.SIMPLIFIED_CHINESE;
}

return super.resolveLocale(request);

}

@Bean
public LocaleResolver localeResolver() {
return this;
}
}

语言工具类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package cn.com.wind.convertserver.utils;

import org.springframework.context.MessageSource;
import org.springframework.context.NoSuchMessageException;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;

import java.util.Locale;


@Component
public class I18nMessageUtils {

private static MessageSource messageSource;

public MessageUtils(MessageSource messageSource) {
MessageUtils.messageSource = messageSource;
}

public static String getMessage(String messageKey, Locale locale) throws NoSuchMessageException {
if (locale == null) {
locale = LocaleContextHolder.getLocale();
}
return messageSource.getMessage(messageKey, null, locale);
}
}

这样就可以通过这个工具类,做国际化的语言转换了。

我们还发现一个问题,通过tomcat容器去运行的应用程序,在request请求里面未能获取到Locale的信息,但把tomcat改为jetty后,是可以成功获取到的。

pom.xml设置如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>

最近在 Linux OpenSUSE 上运行 pdfbox 工具,将 Pdf 转成图片,会遇到找不到字体的警告提示。

1
[15:02:34.732] WARN  org.apache.pdfbox.pdmodel.font.PDTrueTypeFont 220 <init> - Using fallback font 'LiberationSans' for 'Arial,Bold'

所以这篇文章讲述一下如何在 OpenSUSE 系统上安装字体字库。

查看 OpenSUSE 版本号

1
2
3
4
5
6
7
8
9
10
11
# cat /etc/os-release 
NAME="openSUSE Leap"
VERSION="42.3"
ID=opensuse
ID_LIKE="suse"
VERSION_ID="42.3"
PRETTY_NAME="openSUSE Leap 42.3"
ANSI_COLOR="0;32"
CPE_NAME="cpe:/o:opensuse:leap:42.3"
BUG_REPORT_URL="https://bugs.opensuse.org"
HOME_URL="https://www.opensuse.org/"

查看当前系统已经安装的字体

1
2
3
4
5
# fc-list | less

/usr/share/fonts/truetype/DejaVuSans-ExtraLight.ttf: DejaVu Sans,DejaVu Sans Light:style=ExtraLight
/usr/share/fonts/truetype/OpenSans-CondLightItalic.ttf: Open Sans,Open Sans Condensed Light:style=Condensed Light Italic,Italic
...

获取字体

大多数的字体,在 Windows 系统上都可以找到。

字体目录所在位置:C:\Windows\Fonts

安装字体

OpenSUSE 的字库位置在 /usr/share/fonts/truetype,通过刚才命令也可以看出来。

然后再把 Windows 下面的字体拷贝到上述目录中。

重新加载新添加的字体:

1
fc-cache -fv

最后,要让java应用程序生效,还得重新启动一个应用。

0%